MongooseJS - 来自嵌入式文档的$ pullAll值

时间:2014-07-05 12:07:49

标签: javascript mongodb mongoose

目前我有以下方案模型:

var userModel = mongoose.schema({
    images: {
        public: [{url: {type: String}}]}
});

我想删除" images.public"中的项目。来自他们的网址值,而不是他们的ID。

var to_remove = [{url: "a"}, {url: "b"}];
// db entry with id "ID" contains entries with these properties.

userModel.findByIdAndUpdate(ID,
            {$pullAll: {"images.public": to_remove}},
            function(err, doc) {
                if (err) {
                    throw(err);
                }
            }
        );

但是,这不会显示任何错误,也不会删除我要从数据库中删除的项目。我将如何恰当地针对这一目标?

1 个答案:

答案 0 :(得分:2)

// Notice the change of structure in to_remove
var to_remove = ["a", "b"];
userModel.findByIdAndUpdate(ID,
    {$pull: {"images.public": {url: {$in: to_remove}}}},
        function(err, doc) {
            if (err) {
                throw(err);
            }
        }
    );

这里的问题是变量to_remove的结构发生了变化,幸运的是,在我的情况下这不是问题。

相关问题