node.js - 更新在 shell 中工作但不在 Nodejs 驱动程序中工作的查询

标签 node.js mongodb mongoose

此查询正在 mongo shell 中运行

db.orders.updateOne({ _id: ObjectId("5e26be38c13b7149d0a95111"),
"submittedTo":ObjectId("5e2555363405363bc4bf86c2") },
{ $set: {
    ... "vendorOrder" : [
    ...                 {
    ...                         "_id" : ObjectId("5e26be38c13b7149d0a95113"),
    ...                         "publicationCode" : "TOI",
    ...                         "publicationName" : "Times of India",
    ...                         "editionName" : "chennai city",
    ...                         "productCode" : "TCE1",
    ...                         "subscriptionCopies" : 70,
    ...                         "tradeCopies" : 9
    ...                 },
    ...                 {
    ...                         "_id" : ObjectId("5e26be38c13b7149d0a95112"),
    ...                         "publicationCode" : "ET",
    ...                         "publicationName" : "Economic Times",
    ...                         "editionName" : "chennai city",
    ...                         "productCode" : "ECE1",
    ...                         "subscriptionCopies" : 20,
    ...                         "tradeCopies" : 4
    ...                 }
    ...         ]}})

Mongo shell 响应: { "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }

但它无法在nodejs中工作:这是我迄今为止尝试过的 查询1:

exports.editOrder = async (req, res, next) => {
  const { orderId, dealerId, vendorOrder } = req.body;
try {
    const orders = await Order.updateOne(
      {
        _id: orderId,
        submittedTo:dealerId
      },
      { $set: { vendorOrder } }
    );
 res.status(200).json({
      orders,
      message: "order submitted"
    });
  } catch (error) {
    res.send(error);
  }
};

查询2

exports.editOrder = async (req, res, next) => {
  const { orderId, dealerId, vendorOrder } = req.body;

  try {
    const orders = await Order.updateOne(
      {
        _id: mongoose.Types.ObjectId(orderId),
        submittedTo: mongoose.Types.ObjectId(dealerId)
      },
      { $set: { vendorOrder: vendorOrder } }
    );
 res.status(200).json({
      orders,
      message: "order submitted"
    });
  } catch (error) {
    res.send(error);
  }
};

查询3:与双“”其余代码完全相同

const { orderId, dealerId, vendorOrder } = req.body;

      try {
        const orders = await Order.updateOne(
          {
            "_id": mongoose.Types.ObjectId(orderId),
            "submittedTo": mongoose.Types.ObjectId(dealerId)
          },
          { $set: { "vendorOrder": vendorOrder } }
        );

postman JSON:

{
    "orderId":"5e26be38c13b7149d0a95111",
    "submittedTo":"5e2555363405363bc4bf86c2",
    "vendorOrder" : [
                {
                   "_id" : "5e26be38c13b7149d0a95113",
                   "publicationCode" : "TOI",
                   "publicationName" : "Times of India",
                   "editionName" : "chennai city",
                   "productCode" : "TCE1",
                   "subscriptionCopies" : 70,
                   "tradeCopies" : 90
                },
                {
                    "_id" : "5e26be38c13b7149d0a95112",
                    "publicationCode" : "ET",
                    "publicationName" : "Economic Times",
                    "editionName" : "chennai city",
                    "productCode" : "ECE1",
                    "subscriptionCopies" : 20,
                    "tradeCopies" : 40
                }
        ]

postman 响应

{
    "orders": {
        "n": 0,
        "nModified": 0,
        "ok": 1
    },
    "message": "order submitted"
}

最佳答案

两点说明:

查询 #2(或 #3)是正确的 - 您应该始终使用 mongoose.Types.ObjectId(...) 将字符串转换为 ObjectId,因为这两个字段都作为字符串存储在数据库中。

代码的下一个问题是解构:

const { orderId、dealerId、vendorOrder } = req.body;

需要 dealerId,而您的负载包含 subscribedTo 字段,因此您应该尝试:

const { orderId, submittedTo, vendorOrder } = req.body;

const orders = await Order.updateOne(
    {
        _id: mongoose.Types.ObjectId(orderId),
        submittedTo: mongoose.Types.ObjectId(submittedTo)
    },
    { $set: { vendorOrder: vendorOrder } }
);

效果很好(请记住,如果 vendorOrder 变量和存储在数据库中的 vendorOrder 之间没有更改,nModified 将返回为 0

关于node.js - 更新在 shell 中工作但不在 Nodejs 驱动程序中工作的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917877/

相关文章:

mongodb - 如果不存在,则将字段添加到 Mongo 中的文档中

node.js - 为什么我得到的是 AggregationCursor 结果而不是平均值?

javascript - MongoDB-如果不存在则插入,否则跳过

javascript - 能够查询数组中的特定索引吗? Mongoose

javascript - 运行不受信任的用户代码时防止 Node.js 中的系统调用

node.js - 为什么以下文件路径正确....ejs ,nodejs

MongoDB 聚合 - $unwind 顺序文档是否与嵌套数组顺序相同

node.js - 如何用 mongoose 区分填充的集合

sql - 两个用不同语言编写的程序可以连接到同一个 SQL 数据库吗?

node.js - Visual Studio Code 下载 node.d.ts