【问题标题】:MongoDB: Remove JSON from Array in a Document [duplicate]MongoDB:从文档中的数组中删除 JSON [重复]
【发布时间】:2019-06-06 05:38:11
【问题描述】:

我想从文档中的数组中删除 json。 我的文件是这样的:

{
    "_id" : ObjectId("5c2f9a15a372ef8825b18736"),
    "class_section" : [ 
        {
            "class_id" : "1",
            "section_id" : "A"
        }, 
        {
            "class_id" : "2",
            "section_id" : "A"
        }
    ],
    "registered_time" : ISODate("2019-01-04T17:38:29.753Z"),
    "__v" : 0
}

删除 "class_id": "2""section_id": "A" 后,我需要以下这两个条件

{
    "_id" : ObjectId("5c2f9a15a372ef8825b18736"),
    "class_section" : [ 
        {
            "class_id" : "1",
            "section_id" : "A"
        }
    ],
    "registered_time" : ISODate("2019-01-04T17:38:29.753Z"),
    "__v" : 0
}

如何在 MongoDB 中做到这一点?

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    尝试使用 $pull 运算符更新文档

    collection.update(
      {},
      { $pull: { "class_section": { class_id: '2' } } }
    );
    

    请参考$pull操作符here的mongo文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-23
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      相关资源
      最近更新 更多