MongoDB:以BSON(Binary JSON)方式存储数据

Mapping:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

MongoDB入门 随笔 第1张

SQL vs. Mongo(Sampe Query):

MongoDB入门 随笔 第2张

 

MongoDB基础:

MongoDB入门 随笔 第3张

 

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

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄