在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫/ Ruby連接MongoDB操作
MongoDB教程
MongoDB覆蓋查詢
MongoDB數(shù)據(jù)建模
MongoDB聚合
MongoDB更改用戶密碼和自定義數(shù)據(jù)
MongoDB用戶
MongoDB分片
MongoDB創(chuàng)建集合
MongoDB文本搜索
MongoDB數(shù)據(jù)類型
MongoDB GridFS
MongoDB索引限制
MongoDB插入文檔
MongoDB刪除集合
MongoDB數(shù)據(jù)庫引用
MongoDB復(fù)制
MongoDB Map Reduce
Python連接MongoDB操作
MongoDB原子操作
MongoDB特點
MongoDB安全檢查表
MongoDB排序記錄
MongoDB自動遞增序列
MongoDB安裝配置(Windows)
MongoDB備份與恢復(fù)
MongoDB安裝配置(Ubuntu)
Ruby連接MongoDB操作
MongoDB部署
MongoDB索引
MongoDB分析查詢
MongoDB投影(選擇字段)
MongoDB刪除數(shù)據(jù)庫
MongoDB認(rèn)證
MongoDB限制記錄數(shù)
MongoDB添加用戶
MongoDB固定循環(huán)集合
MongoDB高級索引
MongoDB數(shù)據(jù)庫的優(yōu)點
MongoDB快速入門
MongoDB創(chuàng)建數(shù)據(jù)庫
MongoDB啟用身份驗證
MongoDB歷史
MongoDB管理用戶和角色
MongoDB安裝配置(RedHat/CentOS)
MongoDB刪除文檔
Java連接MongoDB操作
MongoDB正則表達(dá)式
MongoDB查詢文檔
MongoDB關(guān)聯(lián)關(guān)系
PHP連接MongoDB操作
MongoDB更新文檔
MongoDB ObjectId

Ruby連接MongoDB操作

MongoDB Ruby驅(qū)動程序是MongoDB官方支持的Ruby驅(qū)動程序。它是用純Ruby編寫的,為了簡化而進(jìn)行了優(yōu)化。它可以自己使用,但它也可以作為幾個對象映射庫的基礎(chǔ)。

1.安裝驅(qū)動程序

Ruby驅(qū)動程序是作為一個gem綁定的,并且托管在Rubygems上。

安裝gem

驅(qū)動程序可以手動或捆綁式安裝。手動安裝gem

gem install mongo

要使用捆綁安裝gem,請在Gemfile中包含以下內(nèi)容:

gem 'mongo', '~> 2.4'

2.前提條件

  • MongoDB實例在localhost上運(yùn)行使用默認(rèn)端口27017。
  • Ruby MongoDB驅(qū)動程序。有關(guān)如何安裝MongoDB驅(qū)動程序的說明,請參閱安裝。
  • 代碼頂部的以下語句:
require 'mongo'

3.Ruby連接MongoDB

使用Mongo::Client建立與正在運(yùn)行的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')

4.訪問數(shù)據(jù)庫和集合

以下示例演示如何訪問指定數(shù)據(jù)庫并顯示其集合:

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]

如果集合不存在,服務(wù)器將在第一次放入數(shù)據(jù)時創(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

該示例應(yīng)該會打印以下內(nèi)容:

{"_id" => 2, "name" => "Sally", "hobbies" => ["skiing", "stamp collecting"]}

更新文件

有幾種更新方法,包括:update_oneupdate_many。 update_one更新單個文檔,而update_many會一次更新多個文檔。

這兩種方法都將查詢過濾器作為第一個參數(shù),以及更新文檔的數(shù)據(jù)作為第二個參數(shù)。 使用$set來添加或更新特定的字段。如果沒有$set,則會將所有文檔更新為給定的更新數(shù)據(jù)。

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_onedelete_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

以下示例將兩個記錄插入到集合中,然后使用與正則表達(dá)式匹配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

創(chuàng)建索引

使用create_onecreate_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 } },
  ])