0

I want to remove a particular chapter from a course from my MongoDB. For example i want to remove "chapterid": 5 from "courseid" 1. This is what i am trying right now:

db.users.update({
            $and:
                [
                    {
                        "email": [email protected]
                    },
                    {
                        "courses.courseid": 1
                    }
                ]
        },
            {
                $pull: {
                    "courses": {
                        "chapterid": 5
                    }
                }
            });

But this query is deleting both the object where there is a chapterid: 5

This is how my DB looks like.

{
        "_id" : ObjectId("5e94393709d9194874cdb1d3"),
        "email" : "[email protected]",
        "courses" : [
                {
                        "courseid" : "1",
                        "chapterid" : 5
                },
                {
                        "courseid" : "2",
                        "chapterid" : 5
                },
                {
                        "courseid" : "2",
                        "chapterid" : 6
                },
        ],
        "__v" : 0
}

Any help is appreciated. Thank You.

2
  • have you tried updatin the "courses.courseid" : "1" because in your collection they are being stored as string not a number Commented Apr 13, 2020 at 10:43
  • just fyi: you can also use: $and condition like this if you have nothing complex in your select query db.users.update({ "email": "[email protected]", "courses.courseid": "1" },{ $pull: { "courses": { "chapterid": 5 } } }); Commented Apr 13, 2020 at 10:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.