mongodb ruby驱动程序是mongodb官方支持的ruby驱动程序。它是用纯ruby编写的,为了简化而进行了优化。它可以自己使用,但它也可以作为几个对象映射库的基础。
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]
如果集合不存在,服务器将在第一次放入数据时创建。
要将单个文档插入到集合中,请使用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
方法一次创建一个索引或多个索引。
client = mongo::client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
collection.indexes.create_one({ name: 1 }, unique: true)
使用create_many
方法使用一个语句创建多个索引。 请注意,使用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 } },
])