MongoDB文檔操作

一、什麼是文檔

MongoDB將數據記錄存儲爲BSON文檔。BSON是JSON的二進制表現形式。所以文檔其實就是一條數據記錄,類似於mysql中的行(row)。(詳見參考資料[1])

二、插入文檔

“插入文檔”指將文檔插入到集合中。

1、語法

(1)db.collection.insert()
(2)db.collection.save()

2、實例

(1)使用db.collection.insert()將文檔插入到名爲student的集合中:

> show collections
log
student
> db.student.insert({
... name:'peter',
... age:18,
... weight:70,
... hobby:['music','reading','tennis']
... })
WriteResult({ "nInserted" : 1 })
>   

(2)db.collection.insert()將文檔插入到名爲student的集合中:

> show collections
log
student
> db.student.save({ name:'alice', age:20, weight:90, hobby:['basketball'] })
WriteResult({ "nInserted" : 1 })
>

3、說明

(1)對於db.collection.insert()方法,如果集合不存在該數據庫中,那麼MongoDB會自動創建該集合,然後再插入文檔。

(2)對於db.collection.save()方法, 如果不指定id字段,那麼就類似於db.collection.insert()方法,如果指定了_id字段,那麼就是更新數據。

三、查看文檔

查看文檔,即查看某一個集合的文檔。

1、語法

db.collection.find()

2、實例

查看名爲student的集合中的文檔:

> show collections
log
student
> db.student.find()
{ "_id" : ObjectId("5e4df0b498e3c0e85a6d10c5") }
{ "_id" : ObjectId("5e4df28db0c755b4f90d37da"), "name" : "peter", "age" : 18, "weight" : 70, "hobby" : [ "music", "reading", "tennis" ] }
{ "_id" : ObjectId("5e4df4fcb0c755b4f90d37db"), "name" : "alice", "age" : 33, "weight" : 50, "hobby" : [ "basketball" ] }
{ "_id" : ObjectId("5e51f02921a55d9a144ec2da"), "name" : "alice", "age" : 20, "weight" : 90, "hobby" : [ "basketball" ] }
>  

四、更新文檔

1、語法

(1)update()方法

db.collection.update(query, update, options)

參數說明:

(a)query: 更新的查詢條件,類似於sql中where命令後面的內容。

(b)update: 更新的內容,類似於sql中set命令後面的內容。

(c)options: 其它可選參數。

對於該方法具體內容參考:https://docs.mongodb.com/manual/reference/method/db.collection.update/

(2)save()方法

需要指定id。

2、實例

將集合student中name爲peter的記錄,將其age修改爲100:

> db.student.update({'name':'peter'},{$set:{'age':'100'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

3、說明

(1)db.collection.update()如果查詢出來的結果有多個,那麼默認只修改一個,如果想讓全部滿足條件的文檔都被修改,那麼需要設置multi參數爲true。

示例:如student集合中name爲alice的文檔有兩條,

> db.student.find()
{ "_id" : ObjectId("5e4df0b498e3c0e85a6d10c5") }
{ "_id" : ObjectId("5e4df28db0c755b4f90d37da"), "name" : "peter", "age" : "100", "weight" : 70, "hobby" : [ "music", "reading", "tennis" ] }
{ "_id" : ObjectId("5e4df4fcb0c755b4f90d37db"), "name" : "alice", "age" : 33, "weight" : 50, "hobby" : [ "basketball" ] }
{ "_id" : ObjectId("5e51f02921a55d9a144ec2da"), "name" : "alice", "age" : 20, "weight" : 90, "hobby" : [ "basketball" ] }

下面想將這兩個文檔的weight都修改爲45,如果不指定multi參數(那麼只有一條記錄被修改):

> db.student.update({'name':'alice'},{$set:{'weight':'100'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

如果指定multi參數,那麼符合條件的文檔都會被修改:

> db.student.update({'name':'alice'},{$set:{'weight':'100'}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

五、刪除文檔

1、語法

db.collection.remove()

2、實例

刪除student集合中name爲alice的文檔記錄:

> db.student.remove({"name":"alice"})
WriteResult({ "nRemoved" : 3 })

3、說明

(1)如果滿足條件的文檔記錄有多個,但是隻想刪除一個,那麼可以指定justOne參數爲true。

> db.student.remove({"name":"alice"},true)
WriteResult({ "nRemoved" : 1 })

六、參考資料

[1]MongoDB官方文檔,Document: https://docs.mongodb.com/manual/core/document/

[2]MongoDB官方文檔,Collection Methods: https://docs.mongodb.com/manual/reference/method/js-collection/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章