![](/img/trans.png)
[英]MongoDB - Mongoose query findOneAndUpdate() doesn't update/duplicates the DB
[英]MongoDB findOneAndUpdate doesn't return errors but also doesn't update the database
我正在嘗試使用 findOneAndUpdate() 但似乎無法讓它工作。 這是我正在使用的查詢。 它將 object 添加到嵌套在“列表”數組中的“項目”數組中,用於具有“用戶名”的特定條目。 我已經為不在嵌套數組內的字段使用簡單的 $set 進行了嘗試,但它似乎也不起作用。 目前沒有返回錯誤。
MongoClient.connect(
uri,
{ useUnifiedTopology: true },
(err, db) => {
if (err) throw err;
var dbo = db.db("Todolist");
dbo.collection("Users").findOneAndUpdate(
{
username: username,
lists: {
$elemMatch: {
name: listName,
},
},
},
{
$push: {
"lists.$.items": {
title: title,
description: description,
dueDate: dueDate,
priority: priority,
completed: false,
},
},
}
);
},
(err, doc) => {
if (err) res.send("/addListItem failed");
else res.send("/addListItem successful");
}
);
} catch (e) {
console.error(e);
}
您必須在此處使用.dot
符號和$
位置運算符
Schema.findOneAndUpdate(
{ username: username },
{ "lists.$.name": listName }
)
事實證明,我的問題的答案是支架放置。 我將 function arguments 的回調(在關閉后)
放置到findOneAndUpdate
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.