0

I want to remove json from an array in a document. My document is this:

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

I need this following after removing "class_id": "2" and "section_id": "A" with both the conditions

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

How can I do this in MongoDB?

1 Answers1

0

Try updating the document by using $pull operator

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

Please refer to mongo documentation of $pull operator here

YoungMonk
  • 63
  • 6