首页 > 数据库技术 > 详细

MongoDB入门

时间:2019-04-20 22:23:49      阅读:173      评论:0      收藏:0      [点我收藏+]

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

 

MongoDB入门

原文:https://www.cnblogs.com/vixennn/p/10742792.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!