简体   繁体   English

如何基于MongoDB中的数组元素数据删除数组元素

[英]How to remove elements of an array based on the array element's data in MongoDB

How to write a Mongo query to remove elements of an array if it contain specific data? 如果包含特定数据,如何编写Mongo查询以删除数组的元素?

{
    "_id": ObjectId("ajdi293akjf83rhfsf398"),
    "one": "oneData",
    "two": [
        {
         "_id":ObjectId("akjf82ijikfj83jkfkj3"),
         "value": 1
        },
        {
         "_id":ObjectId("akjf82ijikfj83jkfkj3"),
         "value": 2
        },
        {
         "_id":ObjectId("akjf82ijikfj83jkfkj3"),
         "value": 1
        }
        ]
}

Need to remove the elements of two if the the elements contains value as 1 如果元素包含的value 1则需要删除two元素

{
    "_id": ObjectId("ajdi293akjf83rhfsf398"),
    "one": "oneData",
    "two": [
        {
         "_id":ObjectId("akjf82ijikfj83jkfkj3"),
         "value": 2
        }
        ]
}

Please tell me how to achieve this Many Thanks in advance. 请提前告诉我如何实现此“非常感谢”。

Update command with $pull operator can do the trick. 使用$ pull运算符进行更新命令可以解决问题。

You can use: 您可以使用:

    db.collection.update( {} ,
                          { $pull: {two : {value : 1} } },
                          { multi: true });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM