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?

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.