日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MongoDB更新文檔
在 MongoDB 中,可以使用 update() 和 save() 方法來更新集合中的文檔。其中 update() 方法可以更新現(xiàn)有文檔中的值,而 save() 方法則可以使用傳入文檔來替換已有文檔。

update() 方法

update() 方法用于更新現(xiàn)有文檔中的值,其語法格式如下:

db.collection_name.update(
    ,
    ,
    {
        upsert: ,
        multi: ,
        writeConcern:
    }
)

參數(shù)說明如下:

  • query:update 的查詢條件,類似 SQL 中 update 語句內(nèi) where 后面的內(nèi)容;
  • update:update 的對象和一些更新的操作符(如 $、$inc...)等,也可以理解為 SQL 中 update 語句內(nèi) set 后面的內(nèi)容;
  • upsert:可選參數(shù),默認值為 false,用來定義當要更新的記錄不存在時,是否當作新記錄插入到集合中,當值為 true 時表示插入,值為 false 時不插入;
  • multi:可選參數(shù),默認值為 false,用來表示只更新找到的第一條記錄,當值為 true 時,則把按條件查出來的多條記錄全部更新;
  • writeConcern:可選參數(shù),用來定義拋出異常的級別。

【示例】首先我們先向集合中插入以下數(shù)據(jù):

> db.course.insert([
... {
...     title: 'MongoDB教程',
...     author: '編程幫',
...     url: 'http://www.biancheng.com/'
... },{
...     title: 'HTML教程',
...     author: '編程幫',
...     url: 'http://www.biancheng.com/html/index.html'
... },{
...     title: 'C#教程',
...     author: '編程幫',
...     url: 'http://www.biancheng.com/csharp/index.html'
... }
... ])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

接著使用 update() 方法修改 title 為“MongoDB教程”的文檔中的內(nèi)容。

> db.course.update({title:"MongoDB教程"},{$set:{url:"http://www.biancheng.net/mongodb/index.html"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.course.find().pretty()
{
        "_id" : ObjectId("603209d8e492ab9be9450304"),
        "title" : "MongoDB教程",
        "author" : "編程幫",
        "url" : "http://www.biancheng.net/mongodb/index.html"
}
{
        "_id" : ObjectId("603209d8e492ab9be9450305"),
        "title" : "HTML教程",
        "author" : "編程幫",
        "url" : "http://www.biancheng.com/html/index.html"
}
{
        "_id" : ObjectId("603209d8e492ab9be9450306"),
        "title" : "C#教程",
        "author" : "編程幫",
        "url" : "http://www.biancheng.com/csharp/index.html"
}

默認情況下,在使用 update() 方法更新文檔時僅會更新一個文檔,若想要更新多個文檔,您則需要將參數(shù)“multi”設置為 true,如下所示:

db.course.update({title:"MongoDB教程"},{$set:{url:"http://www.biancheng.net/mongodb/index.html"}},{multi:true})

save() 方法

MongoDB 中的 save() 方法可以使用傳入的文檔來替換已有文檔,若 _id 主鍵存在則更新已有文檔,若 _id 主鍵不存在則作為新數(shù)據(jù)插入到集合中。語法格式如下:

db.collection_name.save(
    ,
    {
        writeConcern:
    }
)

參數(shù)說明如下:

  • document : 文檔數(shù)據(jù);
  • writeConcern :可選,拋出異常的級別。

【示例】使用 save() 方法更新 _id 為“603209d8e492ab9be9450304”的文檔中的數(shù)據(jù)。

> db.course.save(
... {
... "_id" : ObjectId("603209d8e492ab9be9450304"),
... "title" : "MongoDB教程",
... "description" : "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
... "author" : "編程幫",
... "url" : "http://www.biancheng.net/mongodb/index.html",
... "likes" : 996
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

更新成功后您可以使用 find() 方法來查看更新后的數(shù)據(jù)。

> db.course.find().pretty()
{
        "_id" : ObjectId("603209d8e492ab9be9450304"),
        "title" : "MongoDB教程",
        "description" : "MongoDB 是一個 Nosql 數(shù)據(jù)庫",
        "author" : "編程幫",
        "url" : "http://www.biancheng.net/mongodb/index.html",
        "likes" : 996
}
{
        "_id" : ObjectId("603209d8e492ab9be9450305"),
        "title" : "HTML教程",
        "author" : "編程幫",
        "url" : "http://www.biancheng.com/html/index.html"
}
{
        "_id" : ObjectId("603209d8e492ab9be9450306"),
        "title" : "C#教程",
        "author" : "編程幫",
        "url" : "http://www.biancheng.com/csharp/index.html"
}

分享題目:MongoDB更新文檔
文章鏈接:http://www.dlmjj.cn/article/dhpihpd.html