Squirrl
Squirrl

Reputation: 4966

In mongoDb how do you remove an object within an array that contains a specific key and value?

I'm trying to remove the field from the teacher array that contains a specific subject, such as "ok baby"

{
"_id" : "billy",
"password" : "$2a$10$MKZFNtMhts6rMbnIoqXB9.Q8NHAizQAGhX5S6g.8zeRt7TpRpuQea",
"teacher" : [
    {
        "subject" : "ok baby",
        "students" : [
            "billy"
        ]
    },
    {
        "subject" : "adsfqewr",
        "students" : [
            "billy"
        ]
    }
]
}

This is what I tried:

users.update(      { 'teacher.subject':title, '_id':username},
                   { $pull: { 'teacher.subject':title } },
                   { multi: true }
)

Upvotes: 0

Views: 67

Answers (1)

Muhammad Ali
Muhammad Ali

Reputation: 2014

The query should be like this .,,, pulling data from array is teacher and title is equal to title ...

users.update( { 'teacher.subject':title, '_id':username},
               { $pull: { 'teacher':{'subject':title}} },
               { multi: true }
 );

Upvotes: 1

Related Questions