MongoDB Ruby驅動程序是MongoDB官方支持的Ruby驅動程序。它是用純Ruby編寫的,為了簡化而進行了優(yōu)化。它可以自己使用,但它也可以作為幾個對象映射庫的基礎。
Ruby驅動程序是作為一個gem綁定的,并且托管在Rubygems上。
安裝gem
驅動程序可以手動或捆綁式安裝。手動安裝gem:
gem install mongo
要使用捆綁安裝gem,請在Gemfile中包含以下內容:
gem 'mongo', '~> 2.4'
localhost上運行使用默認端口27017。require 'mongo'
使用Mongo::Client建立與正在運行的MongoDB實例的連接。
require 'mongo'
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
還可以使用URI連接字符串:
require 'mongo'
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
以下示例演示如何訪問指定數據庫并顯示其集合:
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
db = client.database
db.collections # returns a list of collection objects
db.collection_names # returns a list of collection names
要訪問一個集合,請按名稱查看。
collection = client[:restaurants]
如果集合不存在,服務器將在第一次放入數據時創(chuàng)建。
要將單個文檔插入到集合中,請使用insert_one方法。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
doc = { name: 'Steve', hobbies: [ 'hiking', 'tennis', 'fly fishing' ] }
result = collection.insert_one(doc)
result.n # returns 1, because one document was inserted
要將多個文檔插入到集合中,請使用insert_many方法。
docs = [ { _id: 1, name: 'Steve', hobbies: [ 'hiking', 'tennis', 'fly fishing' ] },
{ _id: 2, name: 'Sally', hobbies: ['skiing', 'stamp collecting' ] } ]
result = collection.insert_many(docs)
result.inserted_count # returns 2 because two documents were inserted
使用find方法查詢集合。一個空的查詢過濾器返回集合中的所有文檔。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
collection.find.each do |document|
#=> Yields a BSON::Document.
end
使用查詢過濾器只找到匹配的文檔。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
puts collection.find( { name: 'Sally' } ).first
該示例應該會打印以下內容:
{"_id" => 2, "name" => "Sally", "hobbies" => ["skiing", "stamp collecting"]}
有幾種更新方法,包括:update_one和update_many。 update_one更新單個文檔,而update_many會一次更新多個文檔。
這兩種方法都將查詢過濾器作為第一個參數,以及更新文檔的數據作為第二個參數。 使用$set來添加或更新特定的字段。如果沒有$set,則會將所有文檔更新為給定的更新數據。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
result = collection.update_one( { 'name' => 'Sally' }, { '$set' => { 'phone_number' => "555-555-5555" } } )
puts collection.find( { 'name' => 'Sally' } ).first
使用delete_one或delete_many方法從集合中刪除文檔(單個或多個)。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
result = collection.delete_one( { name: 'Steve' } )
puts result.deleted_count # returns 1 because one document was deleted
以下示例將兩個記錄插入到集合中,然后使用與正則表達式匹配name字段查詢,刪除那些以“S”開頭的字符串的所有文檔。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
collection.insert_many([ { _id: 3, name: "Arnold" }, { _id: 4, name: "Susan" } ])
puts collection.count # counts all documents in collection
result = collection.delete_many({ name: /$S*/ })
puts result.deleted_count # returns the number of documents deleted
使用create_one或create_many方法一次創(chuàng)建一個索引或多個索引。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
collection.indexes.create_one({ name: 1 }, unique: true)
使用create_many方法使用一個語句創(chuàng)建多個索引。 請注意,使用create_many時,語法與create_one不同。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
collection.indexes.create_many([
{ key: { name: 1 } , unique: true },
{ key: { hobbies: 1 } },
])