문제

I got this kind of document

{
    "_id" : "5339be1d9a703ab8708b45675339bed39aac7",
    "description" : "data",
    "name" : "data",
    "members" : [ 
        {
            "user" : {
                "$ref" : "users",
                "$id" : ObjectId("5339be1d9a703ab8708b4567"),
                "$db" : "someDb"
            },
            "type" : "Principal"
        }, 
        {
            "user" : {
                "$ref" : "users",
                "$id" : ObjectId("5339c0c59a703a5d1f8b4569"),
                "$db" : "someDb"
            },
            "type" : "Regular"
        }
    ],
    "owner" : "5339be1d9a703ab8708b4567",
}

And I'm trying to pull an element from the array members, finding it by the $id in user object.

I'm using Mongoose ODM.

This is my function:

>  var conditions = {"_id" : data.guildId},
>      update = 
>      {
>         $pull : 
>         {
>           'members.user.$id' : new mongoose.Types.ObjectId(data.userId) 
>         }
>      };
>      var options = {upsert:false};
> 
>     Guild.update(conditions, update, options, leaveRoom);

There are no errors reported in my node js server or in the mongo log file, but the document remains unaffected.

도움이 되었습니까?

해결책

The pull syntax you have is wrong. $pull takes an array, which in your case is "members".

You want this update instead:

{ "$pull" : { "members" : { "user.$id" : <your-condition> } }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top