Ruby 驱动快速开始
先决条件
运行在本地的MongoDB实例,使用默认端口27017。
Ruby MongoDB驱动程序。请参阅安装说明了解如何安装MongoDB驱动程序。
在代码顶部添加以下语句
require 'mongo'
建立连接
使用Mongo::Client
建立到运行中的MongoDB实例的连接。
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
您还可以使用URI连接字符串
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 db.list_collections # returns a list of collection metadata hashes
要访问集合,请按名称引用它。
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' ], siblings: { brothers: 0, sisters: 1 } } 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' ], siblings: { brothers: 0, sisters: 1 } }, { _id: 2, name: 'Sally', hobbies: ['skiing', 'stamp collecting' ], siblings: { brothers: 1, sisters: 0 } } ] 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"], "siblings" => { "brothers": 1, "sisters": 0 } }
通过指定要匹配的键和值来查询嵌套文档。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test') collection = client[:people] puts collection.find("siblings.sisters": 1 ).first
示例应打印以下内容
{"_id"=>1, "name"=>"Steve", "hobbies"=>["hiking", "tennis", "fly fishing"], "siblings"=>{"brothers"=>0, "sisters"=>1}}
更新文档
有几个更新方法,包括 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
示例应打印以下内容
{"_id" => 2, "name" => "Sally", "hobbies" => ["skiing", "stamp collecting"], "phone_number" => "555-555-5555"}
以下示例使用 update_many
与空查询过滤器来更新集合中的所有文档。
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test') collection = client[:people] result = collection.update_many( {}, { '$set' => { 'age' => 36 } } ) puts result.modified_count # returns 2 because 2 documents were updated
删除文档
使用 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 } }, ])
完整示例应用程序
以下是一个使用Ruby驱动程序针对多个常见用例的示例应用程序,您可以从以下链接下载:GitHub.