无法更新布尔类型的嵌套数组。

huangapple go评论51阅读模式
英文:

Cannot update the nested Array of the boolean

问题

我正在尝试通过使用真或假的语句来更新数组以显示路由器是否正常工作。

但我收到的响应是“记录已更新”,但我无法在MongoDB集合中看到更改。

这是我正在运行的函数

exports = async function({ body }) {
  const data = JSON.parse(body.text());
  const ACAS_Mission = data.ACAS_Mission;
  const terminal = data.terminals[0].terminal;
  const router = data.terminals[0].XLESS.router;

  const comstat = context.services
    .get("mongodb-atlas")
    .db("Comstat")
    .collection("comstat");

  // 查找文档
  const filter = { ACAS_Mission, "terminals.terminal": terminal };
  const document = await comstat.findOne(filter);
  console.log("Document:", JSON.stringify(document));

  // 更新文档
  const updateFilter = { ACAS_Mission, "terminals.terminal": terminal };
  const update = { $set: { "terminals.$[t].XLESS.router": router } };
  const options = { arrayFilters: [{ "t.terminal": terminal }] };
  const result = await comstat.updateOne(updateFilter, update, options);
  console.log("Update result:", JSON.stringify(result));

  return { message: "记录已更新。" };
};

这是我要更新的文档

MongoDB文档

当我进行PUT请求时,我传递的JSON主体如下:

{
  "ACAS_Mission": "xx53583",
  "terminals": [
    {
      "terminal": "SNN573330",
      "XLESS": {
        "router": true
      }
    }
  ]
}
英文:

I am trying to update an array to show the router is up by using a true or false statement.

But I am getting a response back that "record is updated" but I am not able to see the change in MongoDB collection.

This is my function I am running

exports = async function({ body }) {
  const data = JSON.parse(body.text());
  const ACAS_Mission = data.ACAS_Mission;
  const terminal = data.terminals[0].terminal;
  const router = data.terminals[0].XLESS.router;

  const comstat = context.services
    .get("mongodb-atlas")
    .db("Comstat")
    .collection("comstat");

  // Find the document
  const filter = { ACAS_Mission, "terminals.terminal": terminal };
  const document = await comstat.findOne(filter);
  console.log("Document:", JSON.stringify(document));

  // Update the document
  const updateFilter = { ACAS_Mission, "terminals.terminal": terminal };
  const update = { $set: { "terminals.$[t].XLESS.router": router } };
  const options = { arrayFilters: [{ "t.terminal": terminal }] };
  const result = await comstat.updateOne(updateFilter, update, options);
  console.log("Update result:", JSON.stringify(result));

  return { message: "Record updated." };
};

and this is the document I am trying to update

MongoDB Document

The JSON Body that I am passing when I do a PUT

{
  "ACAS_Mission": "xx53583",
  "terminals": [
    {
      "terminal": "SNN573330",
      "XLESS": {
        "router": true
      }
    }
  ]
}

答案1

得分: 0

如您在评论中所写,MongoDB处理了请求,但没有进行任何更改。这意味着您的数据库中没有任何内容与您的查询匹配(可以从matchedCount为0得出这个结论)。

检查您的查询:您的截图显示terminals包含一个包含对象的数组。您正在搜索一个直接包含对象的数组,因此在语句周围添加[方括号]可能会起作用。

英文:

As you wrote in your comment, MongoDB processes the request, but there are no changes. That means that nothing in your datebase matched your query (as one may conclude from matchedCount being 0).

Check your query: Your screenshot shows that terminals contains an array containing another array containing an object. You are searching for an array directly containing an object, so adding [braces] around the statement might work.

答案2

得分: 0

成功修改了JSON主体并进行了一些函数修复,它现在可用!

{
  "ACAS_Mission": "xx53583",
  "terminals": [
    [
      {
        "terminal": "SNN573330",
        "NodeID": 251,
        "XLESS": {
          "router": false
        }
      }
    ]
  ]
}
英文:

Was able to change the JSON body and a few function fixes and it works!

{
  "ACAS_Mission": "xx53583",
  "terminals": [
    [
      {
        "terminal": "SNN573330",
        "NodeID": 251,
        "XLESS": {
          "router": false
        }
      }
    ]
  ]

huangapple
  • 本文由 发表于 2023年2月20日 00:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定