gpt4 book ai didi

javascript - MongooseJS——按值从嵌入式文档中提取所有内容

转载 作者:行者123 更新时间:2023-11-30 17:24:08 24 4
gpt4 key购买 nike

目前我有以下方案模型:

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

我想从“images.public”的 url 值而不是 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);
}
}
);

但是,这不会显示任何错误,也不会从数据库中删除我要删除的项目。我将如何适本地定位它?

最佳答案

// 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 的结构发生了变化,幸运的是这对我来说不是问题。

关于javascript - MongooseJS——按值从嵌入式文档中提取所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com