在Mongo DB中添加和更新子阵列值

发布于 2025-01-29 09:07:07 字数 1735 浏览 3 评论 0原文

我想在Mongo DB集合“ SCDTLS”中的以下JSON结构的数组中添加值。 我需要仅在“ type”:“费用”“ iscurrentversion”:true的情况下添加值。

  {
    "_id": ObjectId("60e71243b484cf3ec22a6845"),
    "Item": "School",
    "Text": "School Details",
    "Types": [
      {
        "Type": "Library",
        "Values": [
          {
            "IsCurrentVersion": false,
            "KeyWords": [
              {
                "_id": ObjectId("611a4f113800d6af3fc4814f"),
                "Value": "Physics"
              }
            ]
          }
        ]
      },
      {
        "Type": "Fees",
        "Values": [
          {
            "IsCurrentVersion": false,
            "KeyWords": [
              {
                "_id": ObjectId("611a4f113800d6af3fc4814f"),
                "Value": "Admission"
              },
              {
                "_id": ObjectId("611a4f113800d6af3fc4814k"),
                "Value": "Canteen"
              }
            ]
          },
          {
            "IsCurrentVersion": true,
            "KeyWords": [
              {
                "_id": ObjectId("611a4f113800d6af3fc4814g"),
                "Value": "Tution"
              }
            ]
          }
        ]
      }
    ]
  }

这就是我尝试的方式

db.Scdtls.update({
  "Item": "School",
  "Types.Type": "Fees",
  "Types.Values.IsCurrentVersion": true
},
{
  "$push": {
    "Types.1.Values.$[].KeyWords": {
      "_id": ObjectId(),
      "Value": "Sports"
    }
  }
})

,但它也添加了“ value”:“ sports” in “ type”:“ fee”:“ fee”其中 “ iscurrentversion”:false < /代码>。 无法理解原因。

I want to add value in array of the following json structure in mongo db collection "Scdtls".
I need to add value only where "Type": "Fees" and "IsCurrentVersion": true.

  {
    "_id": ObjectId("60e71243b484cf3ec22a6845"),
    "Item": "School",
    "Text": "School Details",
    "Types": [
      {
        "Type": "Library",
        "Values": [
          {
            "IsCurrentVersion": false,
            "KeyWords": [
              {
                "_id": ObjectId("611a4f113800d6af3fc4814f"),
                "Value": "Physics"
              }
            ]
          }
        ]
      },
      {
        "Type": "Fees",
        "Values": [
          {
            "IsCurrentVersion": false,
            "KeyWords": [
              {
                "_id": ObjectId("611a4f113800d6af3fc4814f"),
                "Value": "Admission"
              },
              {
                "_id": ObjectId("611a4f113800d6af3fc4814k"),
                "Value": "Canteen"
              }
            ]
          },
          {
            "IsCurrentVersion": true,
            "KeyWords": [
              {
                "_id": ObjectId("611a4f113800d6af3fc4814g"),
                "Value": "Tution"
              }
            ]
          }
        ]
      }
    ]
  }

This is how I am trying

db.Scdtls.update({
  "Item": "School",
  "Types.Type": "Fees",
  "Types.Values.IsCurrentVersion": true
},
{
  "$push": {
    "Types.1.Values.$[].KeyWords": {
      "_id": ObjectId(),
      "Value": "Sports"
    }
  }
})

But it is also adding "Value": "Sports" in "Type" : "Fees" where "IsCurrentVersion": false.
Not able to understand the reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-02-05 09:07:07

如果我正确理解,您可以使用“ arrayfilters”进行更新,例如:

db.Scdtls.update({
  "Item": "School",
  "Types.Type": "Fees",
  "Types.Values.IsCurrentVersion": true
},
{
  "$push": {
    "Types.$[x].Values.$[y].KeyWords": {
      "_id": ObjectId("deadbeafcafebabec0deface"),
      "Value": "Sports"
    }
  }
},
{
  "arrayFilters": [
    { "x.Type": "Fees" },
    { "y.IsCurrentVersion": true }
  ]
})

mongoplayground.net

If I understand correctly, you can do the update you want using "arrayFilters", like this:

db.Scdtls.update({
  "Item": "School",
  "Types.Type": "Fees",
  "Types.Values.IsCurrentVersion": true
},
{
  "$push": {
    "Types.$[x].Values.$[y].KeyWords": {
      "_id": ObjectId("deadbeafcafebabec0deface"),
      "Value": "Sports"
    }
  }
},
{
  "arrayFilters": [
    { "x.Type": "Fees" },
    { "y.IsCurrentVersion": true }
  ]
})

Try it on mongoplayground.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文