MongoDB $用于嵌套文档的操作员

发布于 2025-01-26 16:48:15 字数 841 浏览 4 评论 0原文

有一个WFS集合如下:

{
_id:'1',
transitions:
  [
    {
     _id:'11',
     checkLists:
       [
        {
         _id:'111',
         name:'One',
        },
        {
         _id:'112',
         name:'Two',
        }
       ]
     }
  ]
}

我想获得_id的子文件:'111' 我已经尝试了以下代码,但没有按预期工作,它返回了所有第二级嵌套文档而不是正确的对象

db.wfs.findOne(
    { 'transitions.checkLists._id':  ObjectId('111') },
    { 'transitions.checkLists._id': 1 },
  );

结果:

{
transitions: [
  {
    "checkLists": [
      {
        "name": "One",
        "_id": "111"
      },
      {
        "name": "Two",
        "_id": "112"
      }
    ]
  }
]
}

预期结果:

[
  {
    "checkLists": [
      {
        "name": "One",
        "_id": "111"
      }
    ]
  }
]

赞赏任何提示或解决方案

there is a wfs collection as follows:

{
_id:'1',
transitions:
  [
    {
     _id:'11',
     checkLists:
       [
        {
         _id:'111',
         name:'One',
        },
        {
         _id:'112',
         name:'Two',
        }
       ]
     }
  ]
}

I would like to get the sub sub document of _id:'111'
I have tried the following code but not working as expected, it returns all of 2nd level nested documents not the proper object

db.wfs.findOne(
    { 'transitions.checkLists._id':  ObjectId('111') },
    { 'transitions.checkLists._id': 1 },
  );

result:

{
transitions: [
  {
    "checkLists": [
      {
        "name": "One",
        "_id": "111"
      },
      {
        "name": "Two",
        "_id": "112"
      }
    ]
  }
]
}

Expected result:

[
  {
    "checkLists": [
      {
        "name": "One",
        "_id": "111"
      }
    ]
  }
]

appreciated any hint or solution

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

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

发布评论

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

评论(1

无名指的心愿 2025-02-02 16:48:15

您可以使用:

db.collection.aggregate([
  {
    $match: {"transitions.checkLists._id": "111"}
  },
  {
    $project: {
      transitions: {
        $reduce: {
          "input": "$transitions",
          initialValue: [],
          in: {$concatArrays: ["$value", "$this.checkLists" ]}
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      checkLists: {
        $filter: {
          input: "$transitions",
          as: "item",
          cond: {$eq: ["$item._id",  "111" ]}
        }
      }
    }
  }
])

此游乐场示例

$降低用于“弄平”您的列表,$ filter用于仅保留所需的零件。

这是基于 @rickhg12hs < /a>基本上相似但更复杂的问题。这里的解决方案只是一个简单的版本。

You can use:

db.collection.aggregate([
  {
    $match: {"transitions.checkLists._id": "111"}
  },
  {
    $project: {
      transitions: {
        $reduce: {
          "input": "$transitions",
          initialValue: [],
          in: {$concatArrays: ["$value", "$this.checkLists" ]}
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      checkLists: {
        $filter: {
          input: "$transitions",
          as: "item",
          cond: {$eq: ["$item._id",  "111" ]}
        }
      }
    }
  }
])

As you can see on this playground example.

The $reduce is used to "flatten" your list, and the $filter is used to keep only the part you want.

This is based on this solution by @rickhg12hs to a basically similar, but little more complex, problem. The solution here is just a simple version of it.

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