使用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

 

转载于:https://www.cnblogs.com/RDaneelOlivaw/p/7860107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值