簡體   English   中英

MongoDB findOneAndUpdate 不返回錯誤但也不更新數據庫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM