MongoDB $ pull request does not delete and update existing record

My main problem is that in the mongodb documentation they use a command $elemMatchto find and pull an element from an array, but this does not work when I use. My document structure

{
"_id" : ObjectId("55dea445c3ad8cac03a7ed8e"),
"email" : "james@user.com",
"groups" : [ 
    {
        "_id" : ObjectId("55dea4a4c3ad8c8005a7ed8f"),
        "name" : "All Group"
    }
]}

I want to remove groups from a document using mongodb request. My request:

db.users.update({"_id": ObjectId('55dea445c3ad8cac03a7ed8e')}, 
{$pull: {"groups": {$elemMatch: {"_id": ObjectId('55dea4a4c3ad8c8005a7ed8f')}}}})

After the request is completed, the user document is not updated and the group value is still saved. I follow the mongodb documentation as: http://docs.mongodb.org/v2.6/reference/operator/update/pull/

+4
source share
1 answer

$elemMatch $pull. "" :

db.users.update(
    {"_id": ObjectId('55dea445c3ad8cac03a7ed8e')}, 
    { "$pull": { "groups": {"_id": ObjectId('55dea4a4c3ad8c8005a7ed8f')}}}
)

, $pull , $elemMatch , .

, , .

+8

All Articles