mongo 删除内嵌数组元素

来源:互联网 发布:js选择器innerhtml 编辑:程序博客网 时间:2024/06/06 06:29

文档格式如下:

{    "_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
0 0