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