MongoDB入门
时间:2019-04-20
本文章向大家介绍MongoDB入门,主要包括MongoDB入门使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
MongoDB:以BSON(Binary JSON)方式存储数据
Mapping:
SQL vs. Mongo(Sampe Query):
MongoDB基础:
MongoDB Ruby Driver Setup:
mongo-ruby driver
*gem update -system
*gem install mongo
*gem install bson_ext
Using gem:
*require mongo
精简信息:
Mongo::Logger.logger.level = ::Logger::INFO
Inserting Documents:
#insert_one: insert one document to collection db[:zips].insert_one(:_id => "100", :city => "city01") db[:zips].find(:city => "city01").count #insert_many: insert multiple documents to the collection db[:zips].insert_many([{:_id => "200", :city => "city02"},{:_id => "201", :city => "city03"}]) # _id - primary key for every document # default field for the BSON object and is indexed # you can add a custom "id" field if you like
find("R"ead in CRUD):
find - returns a cursor object - allows us to iterate over the selected documents
db[zips].find(:city => "BALTIMORE") #return first city named BALTIMORE db[zips].find(:city => "BALTIMORE") .first # return distinct data from the database db[:zips].find.distinct(:state) #return the count db[zips].find(:city => "BALTIMORE") .count #return pp include multiple info require 'pp' pp db[zips].find(:city => "BALTIMORE", :state => "NY") .first #PRINT ALL db[:zips].find().each { |r| puts r } #pretty printing require 'pp' db[:zips].find().each { |r| pp r } # projections: selecting only necessary data. # true or 1: inclusive # false or 0: exclusive db[zips].find({:state=> "MD"}).projection(state:true).first => {"_id" => "20331", "state" => "MD" } db[zips].find({:state=> "MD"}).projection(state:true, _id:false).first => {"state" => "MD"}
Paging:
skip(n) - 跳过n个结果
limit(n) - 限制只返回n个结果
# retrieves a list of the first three documents for us db[:zips].find.limit(3).each { |r| pp r} # skip first n document db[:zips].find.skip(n).each { |r| pp r} #sort 1 for ascending db[:zips].find.limit(3)..sort({:city => 1}).each { |r| pp r} #sort -1 for descending db[:zips].find.limit(3).sort({:city => -1}).each{ |r| pp r}
advanced find:
#Find controls with lt(less than) and gt(greater than) operator db[:zips].find(:city => { :$lt => 'D'}).limit(2).to_a.each { |r| pp r} #Find By - Regex db[:zips].find(:city => { :$regex=> 'X'}).limit(5).to_a.each { |r| pp r} #return regex end with X db[:zips].find(:city => { :$regex=> 'X$'}).limit(5).to_a.each { |r| pp r} #return regex start with X db[:zips].find(:city => { :$regex=> '^X'}).limit(5).to_a.each { |r| pp r} #return regex in range db[:zips].find(:city => { :$regex=> '^[A-E]'}).limit(5).to_a.each { |r| pp r} # check the document exists when the boolean is true db[:zips].find(:city => { :$exists=> true}).limit(5).to_a.each { |r| pp r} # $not performs a logical NOT operation db[:zips].find(:city => { :$pop=> '$not' => {'$gt' => 9500}}).limit(5).to_a.each { |r| pp r} #type - selects the documents where the value of the field is an instance of the specified numeric BSON type #Handy when dealing with unstructured data where data types are not predictable db[:zips].find({ :state => {'$type => 2'}}).first #check types on 'mongodb.org/v3.0/reference/bson types/'
RUD:
# replace_one - Replace a document in the collection db[:zips].find(:_id => "100").replace_one(:_id => "100", :city => 'city2" ) #update_one db[:zips].find(:_id => "100").update_one(:$set => {:city => "name2"}) #update_many db[:zips].find(:state=> "MD").update_many(:$set => {state: "XX"}) #delete_one db[:zips].find(:_id => "100").delete_one() #delete_many db[:zips].find(:state => "MD").delete_many() #upsert db[:zips].find(:city => "ODENVILLE2).count => 0 db[:zips].find(:city => "ODENVILLE2).update_one({:$set => {:city => "ODENVILLE2"}}, :upsert => true) db[:zips].find(:city => "ODENVILLE2).count => 1
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 老哥,您看我这篇Java集合,还有机会评优吗?
- 在 IDEA 中使用 Java 热部署插件 JRebel
- 后端必备 Git 分支开发:规范指南
- 大量的 TIME_WAIT 状态连接怎么处理?(文末有福利)
- 线上教育系统开发中,购物车功能是如何实现的?
- IntelliJ IDEA 2020.2重磅发布!全面支持Github PR。真香版本?
- 腾讯云智能语音小程序插件实现实时语音识别
- 我还在生产玩 JDK7,JDK 15 却要来了!|新特性尝鲜
- 36个助你成为专家需要掌握的JavaScript概念
- Bulehero 蠕虫病毒安全分析报告
- 一句话加速grep近30倍
- 正道的光!这有个用TensorFlow做的小黄图过滤器
- Bytom侧链Vapor源码浅析-节点出块过程
- Logstash: 启动监控及集中管理
- 渗透测试中文件上传技巧