Вопрос

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