mongo 刪除內嵌數組元素


文檔格式如下:

{
    "_id" : ObjectId("56e2a92ccc6dd2271953e502"),
    "links": [
        {
            "name": "Google",
            "url": "http://www.google.com"
        },
        {
            "name": "Baidu",
            "url": "http://www.baidu.com"
        },
        {
            "name": "SoSo",
            "url": "http://www.SoSo.com"
        }
    ]
}

要刪除 links 中 name 是 Baidu 的記錄

db.collection.update(
  { _id: ObjectId('id') },
  { $pull: { links: { name: 'Baidu' } } }
);

根據索引刪除,索引從 0 開始

db.collection.update(
  { _id: ObjectId('id') },
  { $unset: { 'links.1': 1 } }
);

原來的值會替換為 null

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM