【发布时间】:2022-06-10 18:42:51
【问题描述】:
我可以看到这个问题应该在这里得到回答,但是代码根本不适合我(我尝试了多种类似的变体)。
这是我的数据:
[{
"_id": {
"$oid": "628cadf43a2fd997be8ce242"
},
"dcm": 2,
"status": true,
"comments": [
{
"id": 289733,
"dcm": 2,
"status": true,
"clock": "158",
"user": "Nathan Field",
"dept": "IT",
"department": [],
"dueback": "",
"comment": "test 1"
},
{
"id": 289733,
"dcm": 2,
"status": true,
"clock": "158",
"user": "Nathan Field",
"dept": "IT",
"department": [],
"dueback": "",
"comment": "test 2"
}
],
"department": [],
"dueback": ""
}]
这是我的代码
const deleteResult = await db.collection('status').updateOne(
{ "dcm": comments.dcm },
{ $pull: { "comments": { "id": comments.id } } },
{ upsert: false },
{ multi: true }
);
什么都没有发生……
【问题讨论】:
-
我刚刚尝试了您的数据和更新表达式,它在匹配
dcm后从comments数组中删除了目标id。我怀疑你传入的参数不是他们看起来的那样。 -
检查数据类型确定,可能
dcm和comments.id是字符串,默认也是upsert false,如果你选择了updateOne,去掉multi true。如果要更新很多,请使用 updateMany -
dcm和comments.id在 MongoDB Compass 中都显示为 Int32。我在所有变体中都尝试了带撇号和不带撇号。
标签: node.js arrays mongodb object