MongoDB中對文檔的增刪查改基本操作方法總結_MongoDB

來源:互聯網
上載者:User

插入文檔:insert() 方法

要插入資料到 MongoDB 集合,需要使用 MongoDB 的  insert() 或 save() 方法。

文法:

insert() 命令的基本文法如下:

 >db.COLLECTION_NAME.insert(document)

例子:

 >db.mycol.insert({    _id: ObjectId(7df78ad8902c),    title: 'MongoDB Overview',     description: 'MongoDB is no sql database',    by: 'tutorials point',    url: 'http://www.jb51.net',    tags: ['mongodb', 'database', 'NoSQL'],    likes: 100 })
這裡 mycol  是集合的名稱,如前面的教程中建立。如果集合在資料庫中不存在,那麼MongoDB 將建立此集合,然後把它插入文檔。

插入文檔中,如果我們不指定_id參數,然後MongoDB 本文檔分配一個獨特的ObjectId。

_id 是12個位元組的十六進位數,唯一一個集合中的每個文檔。 12個位元組被劃分如下:

 _id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer)

要插入單個查詢的多個文檔,可以傳遞一個數組 insert() 命令的檔案。

樣本:

 >db.post.insert([ {    title: 'MongoDB Overview',     description: 'MongoDB is no sql database',    by: 'tutorials point',    url: 'http://www.jb51.net',    tags: ['mongodb', 'database', 'NoSQL'],    likes: 100 }, {    title: 'NoSQL Database',     description: 'NoSQL database doesn't have tables',    by: 'tutorials point',    url: 'http://www.jb51.net',    tags: ['mongodb', 'database', 'NoSQL'],    likes: 20,     comments: [        {          user:'user1',          message: 'My first comment',          dateCreated: new Date(2013,11,10,2,35),          like: 0        }    ] } ])
要插入檔案,也可以使用  db.post.save(document)。 如果不指定_id在文檔中,然後將其 save() 方法和 insert()方法工作一樣。如果指定_id,它會替換整個資料檔案,其中包含_id 指定save()方法。


刪除文檔:remove() 方法

MongoDB的 remove() 方法用於從集合中刪除文檔。remove() 方法接受兩個參數。第一個是刪除criteria ,第二是justOne標誌:

(1)deletion criteria :(可選)刪除標準,根據檔案將被刪除。

(2)justOne : (可選)如果設定為true或1,然後只刪除一個檔案。

文法:

基本文法remove()方法如下

 >db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

例子:

考慮以下資料mycol集合。

 { "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"}
下面的例子將刪除所有的檔案,其標題是 'MongoDB Overview'

 >db.mycol.remove({'title':'MongoDB Overview'}) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"} >
刪除只有一個。

如果有多個記錄且要刪除的只有第一條記錄,那麼設定remove()方法中justOne參數

 >db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)

刪除所有檔案:

如果不指定刪除條件,然後MongoDB將從集合中刪除整個檔案。這相當於SQL的truncate命令。

 >db.mycol.remove() >db.mycol.find() >


查詢文檔:
1.find() 方法
要從MongoDB 查詢集合資料,需要使用MongoDB 的 find() 方法。

文法:

基本的find()方法文法如下

 >db.COLLECTION_NAME.find()

find() 方法將在非結構化的方式顯示所有的檔案。

2.pretty() 方法

結果顯示在一個格式化的方式,可以使用 pretty() 方法.

文法:

 >db.mycol.find().pretty()

例子:

 >db.mycol.find().pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "tutorials point",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >
除了find() 方法外,還有一個 findOne() 法,返回一個檔案。

RDBMS Where子句和MongoDB等同語句.

要查詢檔案的一些條件的基礎上,可以使用下面的操作

操作 文法 例子 RDBMS 等同
Equality key db.mycol.find({"by":"tutorials point"}).pretty() where by = 'tutorials point'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50

3.AND 在MongoDB中用法

文法:

在  find() 方法,如果通過多個鍵分離',',那麼 MongoDB 處理 AND 條件。AND 基本文法如下所示:

 >db.mycol.find({key1:value1, key2:value2}).pretty()

例子

下面給出的例子將顯示所有的教程,標題是“MongoDB Overview“

 >db.mycol.find({"by":"tutorials point","title": "MongoDB Overview"}).pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "yiibai",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >
對於上面給出的例子相當於where子句 ' where by='yiibai' AND title='MongoDB Overview' , 可以通過任意數量的索引值對在 find 子句。

4.MongoDB中OR

文法:

OR條件的基礎上要查詢檔案,需要使用$or關鍵字。OR 基本文法如下所示: 

 >db.mycol.find(  {    $or: [    {key1: value1}, {key2:value2}    ]  } ).pretty()

例子

下面給出的例子將顯示所有的教程,由'yiibai' 所寫或標題是“MongoDB Overview '

 >db.mycol.find({$or:[{"by":"yiibai"},{"title": "MongoDB Overview"}]}).pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "yiibai",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >
5.AND 和 OR 一起使用

例子

下面給出的例子將顯示有像的檔案大於100,其標題是“MongoDB Overview'或者是'yiibai' 。等效於 SQL where子句 為 

'where likes>10 AND (by = 'yiibai' OR title = 'MongoDB Overview')'

 >db.mycol.find("likes": {$gt:10}, $or: [{"by": "yiibai"}, {"title": "MongoDB Overview"}] }).pretty() {    "_id": ObjectId(7df78ad8902c),    "title": "MongoDB Overview",     "description": "MongoDB is no sql database",    "by": "yiibai",    "url": "http://www.jb51.net",    "tags": ["mongodb", "database", "NoSQL"],    "likes": "100" } >

 

 更新文檔
 MongoDB的 update() 和 save() 方法用於更新文檔的集合。 update()方法更新現有的文檔值,而替換現有的文檔通過的檔案中 save() 方法。

1.MongoDB Update() 方法

update()方法更新現有文檔值。

文法:

update() 方法的基本文法如下

 >db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

例子

考慮以下資料mycol集合。

 { "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
下面的例子將設定新標題'MongoDB Overview'的檔案,更新其標題是“New MongoDB Tutorial”

 >db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}}) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"} >
MongoDB預設將只更新單一的檔案,來更新多個你需要設定參數置'multi' 為true

 >db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
2.MongoDB Save() 方法

 save() 方法替換現有的文檔和通過新的文檔 save() 方法

文法

MongoDB 的 save() 方法的基本文法如下:

 >db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

例子

下面的例子將取代檔案具有_id為 '5983548781331adf45ec7'

 >db.mycol.save(    {       "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai New Topic", "by":"Yiibai"    } ) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec5), "title":"Yiibai New Topic", "by":"Yiibai"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"} >
  

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.