簡體   English   中英

獲取 Mongoose UpdateMany 受影響文檔的計數

[英]Get count of affected documents of Mongoose UpdateMany

假設我們有代碼:

 await Employees.updateMany(
        {
          InsertDate: {
            $gte: _yesterday,  // date ojbect that is passed as a param
            $lte: _today  // date ojbect that is passed as a param
          }
        },
        { $set: { RegistrationDate: ... // some var that I generate } }
      );

我們怎樣才能得到受影響文件的數量(計數)?

由於在記錄文檔,你可以得到響應的更新文件的數量nModified字段。

所以你可以像這樣訪問它:

const response = await Employees.updateMany(
  {
    InsertDate: {
      $gte: _yesterday,  // date ojbect that is passed as a param
      $lte: _today  // date ojbect that is passed as a param
    }
  },
  { $set: { RegistrationDate: ... // some var that I generate } }
);

console.log("Number of updated documents: ", response.nModified);

如果您使用的是 MongoDB 版本 >= 5.0,則使用modifiedCount而不是nModified

例如

const response = await Employees.updateMany(
  {
    InsertDate: {
      $gte: _yesterday,  // date ojbect that is passed as a param
      $lte: _today  // date ojbect that is passed as a param
    }
  },
  { $set: { RegistrationDate: ... // some var that I generate } }
);

console.log("Number of updated documents: ", response.modifiedCount);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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