mongodb-在嵌套数组中找到

发布于 2025-01-23 21:04:11 字数 1829 浏览 0 评论 0原文

我的目标是:

  1. status = true获取所有文档。
  2. 并仅在life数组中使用active = true返回对象。

以下是我的mongoDB文档的样子:

{
    "name": "justine",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : false
                }
        }
    ]
},
{
    "name": "okumu",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : true
                }
        }
    ]
}

预期输出:

{
    "name": "justine",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }
        }
    ]
},
{
    "name": "okumu",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : true
                }
        }
    ]
}

这是我所做的:

await Users.find({ life: { $elemMatch: { status: true, life: { $elemMatch: { active: false } } } }});

这对于第一个条件很好,如果不满足第二个条件,则没有返回整个对象,但是,如果满足,即使是active = false对象将返回。

如果您能帮助我,我将不胜感激,不是MongoDB专家。

My goal is to:

  1. Get all the documents with status=true.
  2. And return only objects with active=true in the life array.

Below is what my MongoDB documents look like:

{
    "name": "justine",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : false
                }
        }
    ]
},
{
    "name": "okumu",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : true
                }
        }
    ]
}

Expected output:

{
    "name": "justine",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }
        }
    ]
},
{
    "name": "okumu",
    "life" : [ 
        {
            "status" : true,
            "service" : [ 
                {
                    "partner" : "pat 1",
                    "active" : true,
                }, 
                {
                    "partner" : "pat 2",
                    "active" : true
                }
        }
    ]
}

This is what I did:

await Users.find({ life: { $elemMatch: { status: true, life: { $elemMatch: { active: false } } } }});

This is working well for the first condition, in case the second condition is not met, the entire object is not returned, however, if it's met, even the active=false objects are returned.

I'll be grateful if you can help me out, am not a MongoDB expert.

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

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

发布评论

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

评论(1

电影里的梦 2025-01-30 21:04:11

我认为使用.find()查询很复杂(可能无法可行)。

您应该使用.aggregate()查询。

  1. $ match - 带有life.status的过滤文档 true 。

  2. $ project -

    2.1 $ filter - 用状态 true 在life> life array document。

    2.1.1 $ map - 合并life文档与结果 2.1.1.1 。。

    2.1.1.1 $ filter - 内部$ filter操作员用active> active is true true 服务 服务>服务数组中的文档。

db.collection.aggregate([
  {
    $match: {
      "life.status": true
    }
  },
  {
    $project: {
      name: 1,
      life: {
        "$filter": {
          "input": {
            "$map": {
              "input": "$life",
              "in": {
                "$mergeObjects": [
                  "$this",
                  {
                    "service": {
                      "$filter": {
                        "input": "$this.service",
                        "as": "service",
                        "cond": {
                          $eq: [
                            "$service.active",
                            true
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          },
          as: "life",
          "cond": {
            $eq: [
              "$life.status",
              true
            ]
          }
        }
      }
    }
  }
])

示例mongo playground

I think it is complex (possible not doable) with the .find() query.

You should use .aggregate() query.

  1. $match - Filter document with life.status is true.

  2. $project -

    2.1 $filter - Filter the document with status is true in life array document.

    2.1.1 $map - Merge the life document with the Result 2.1.1.1.

    2.1.1.1 $filter - The inner $filter operator to filter the document with active is true for service document in service array.

db.collection.aggregate([
  {
    $match: {
      "life.status": true
    }
  },
  {
    $project: {
      name: 1,
      life: {
        "$filter": {
          "input": {
            "$map": {
              "input": "$life",
              "in": {
                "$mergeObjects": [
                  "$this",
                  {
                    "service": {
                      "$filter": {
                        "input": "$this.service",
                        "as": "service",
                        "cond": {
                          $eq: [
                            "$service.active",
                            true
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          },
          as: "life",
          "cond": {
            $eq: [
              "$life.status",
              true
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

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