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": roshan_raj@gmail.com
                    },
                    {
                        "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" : "roshan_raj@gmail.com",
        "courses" : [
                {
                        "courseid" : "1",
                        "chapterid" : 5
                },
                {
                        "courseid" : "2",
                        "chapterid" : 5
                },
                {
                        "courseid" : "2",
                        "chapterid" : 6
                },
        ],
        "__v" : 0
}

Any help is appreciated. Thank You.

Roshan Raj
  • 173
  • 2
  • 10
  • have you tried updatin the `"courses.courseid" : "1"` because in your collection they are being stored as string not a number – Abhishek Jain Apr 13 '20 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": "roshan_raj@gmail.com", "courses.courseid": "1" },{ $pull: { "courses": { "chapterid": 5 } } });` – Abhishek Jain Apr 13 '20 at 10:46

0 Answers0