使用MongoDB ruby驱动进行简单连接/CRUD/运行命令

1. 连接数据库

require 'mongo'

#access database 'test'
client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
db = client.database

db.collections  #return a list of collection objects
db.collections.name #return a list of collection names

collection = client[:restaurant] #access collection 'restaurant'

 

 

2. 插入文档

a. 插入单个文档

require 'mongo'

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')

collection = client[:people] #acess collection 'people'

#insert a single document into the collection
doc = { name: 'Steve', hobbie: ['hiking', 'tennis', 'fly fishing'] }
result = collection.insert_one(doc)
result.n #returns 1

b. 插入多个文档

require 'mongo'
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]


#insert multiple documents into a collection
docs = [ {_id: 1, name: 'Steve', hobbies: ['hiking', 'tennis', 'fly fishing'] }, {_id: 2, name: 'Sally', hobbies: ['skiing', 'stamp collecting'] } ]

result = collection.insert_many(docs)
p result.inserted_count #returns 2

 

3. 查询文档

a. 查询所有文档

require 'mongo'
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]

collection.find. each do |document|
    puts document
end

b. 筛选查询

require 'mongo'
client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]
puts collection.find({name: 'Sally'} ).first

 

4. 更新文档

a. 更新单个文档

require 'mongo'

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]

result = collection.update_one({name:'Steve'}, 
    {'$set':{phone_number: "444-444-4444"} })
puts collection.find({name:'Steve'}).first

b. 更新多个文档

require 'mongo'

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

 

5. 删除文档

a. 删除单个文档

require 'mongo'

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]

result = collection.delete_one(name:'Steve')
puts result.deleted_count

b. 删除多个文档

require 'mongo'

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

result = collection.delete_many({name:/$S*/})
puts result.deleted_count

 

6. 创建索引

a. 创建单个索引

require 'mongo'

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')
collection = client[:people]

collection.indexes.create_one({name:1}, unique:true)

b. 创建多个索引

require 'mongo'

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} }
  ])

 

7. 运行数据库命令

require 'mongo'
#connect to database 'admin'
client = Mongo::Client.new('mongodb://127.0.0.1:27017/admin')
db = client.database

#command 'listDatabases'
p result = db.command({listDatabases:1})

 

8. 删除(drop)集合/数据库

a. 删除集合

require 'mongo'

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')

collection = client[:people]
collection.drop

b. 删除数据库

require 'mongo'

client = Mongo::Client.new('mongodb://127.0.0.1:27017/test')

client.database.drop

 

参考: MongoDB Ruby Driver Manual - Quick Start

 

posted @ 2017-11-19 16:08  丹尼尔奥利瓦  阅读(282)  评论(0编辑  收藏  举报