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