嵌套数组聚集仅用于匹配一个字段

发布于 2025-01-18 07:36:23 字数 2918 浏览 0 评论 0原文

我在 mongodb 中有以下 json 数据。

[
  {
    "_id": 1,
    "name": "user1",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 2,
    "name": "user2",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 3,
    "name": "user3",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 4,
    "name": "user4",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 5,
    "name": "user5",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 6,
    "name": "user6",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 7,
    "name": "user7",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 8,
    "name": "user8",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  }
]

我正在查询分页数据,如下所示。

[
    {
        "$match": {
            "userActivities": {
                "actionTaken": "Sign in."
            }
        }
    },
    {
        "$sort": {
            "name": -1
        }
    },
    {
        "$project": {
            "name": 1,
            "userActivities": {
                "$filter": {
                    "input": "$userActivities",
                    "as": "userActivities",
                    "cond": {
                        "$eq": [
                            "$$userActivities.actionTaken",
                            "Sign in."
                        ]
                    }
                }
            }
        }
    },
    {
        "$skip": 2
    },
    {
        "$limit": 2
    }
]

但即使符合过滤条件,也无法获取任何数据。如果我从示例数据中删除 progress 字段,那么它会为我提供数据。

当要查询的列中有多个字段时,是否应该采取不同的做法?

I have following json data in mongodb.

[
  {
    "_id": 1,
    "name": "user1",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 2,
    "name": "user2",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 3,
    "name": "user3",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 4,
    "name": "user4",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 5,
    "name": "user5",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 6,
    "name": "user6",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 7,
    "name": "user7",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  },
  {
    "_id": 8,
    "name": "user8",
    "userActivities": [
      {
        "actionTaken": "Sign in.",
        "progress": "Success."
      },
      {
        "actionTaken": "logout",
        "progress": "Success."
      }
    ]
  }
]

I am querying data for pagination as following.

[
    {
        "$match": {
            "userActivities": {
                "actionTaken": "Sign in."
            }
        }
    },
    {
        "$sort": {
            "name": -1
        }
    },
    {
        "$project": {
            "name": 1,
            "userActivities": {
                "$filter": {
                    "input": "$userActivities",
                    "as": "userActivities",
                    "cond": {
                        "$eq": [
                            "$userActivities.actionTaken",
                            "Sign in."
                        ]
                    }
                }
            }
        }
    },
    {
        "$skip": 2
    },
    {
        "$limit": 2
    }
]

But not getting any data even though it matches filter criteria. If I remove progress field from sample data then it gives me data.

Is it something should be done differently when multiple fields are there in column to query?

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

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

发布评论

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

评论(3

人间☆小暴躁 2025-01-25 07:36:23

在您的$ Match阶段中使用点符号。

{
  "$match": {
    "userActivities.actionTaken": "Sign in."
  }
}

示例mongo playground

Use dot notation in your $match stage.

{
  "$match": {
    "userActivities.actionTaken": "Sign in."
  }
}

Sample Mongo Playground

胡渣熟男 2025-01-25 07:36:23

通过一个人的帮助得到答案。我不得不使用$ lemmatch如下。

    [
      {
        "$match": {
          "userActivities": {
            "$elemMatch": {
              "actionTaken": "Sign in."
            }
          }
        }
      },
      {
        "$sort": {
          "name": -1
        }
      },
      {
        "$project": {
          "name": 1,
          "userActivities": {
            "$filter": {
              "input": "$userActivities",
              "as": "userActivities",
              "cond": {
                "$eq": ["$userActivities.actionTaken", "Sign in."]
              }
            }
          }
        }
      },
      {
        "$skip": 2
      },
      {
        "$limit": 2
      }
    ]

Got answer by taking help from one person. I had to use $elemMatch as following.

    [
      {
        "$match": {
          "userActivities": {
            "$elemMatch": {
              "actionTaken": "Sign in."
            }
          }
        }
      },
      {
        "$sort": {
          "name": -1
        }
      },
      {
        "$project": {
          "name": 1,
          "userActivities": {
            "$filter": {
              "input": "$userActivities",
              "as": "userActivities",
              "cond": {
                "$eq": ["$userActivities.actionTaken", "Sign in."]
              }
            }
          }
        }
      },
      {
        "$skip": 2
      },
      {
        "$limit": 2
      }
    ]
誰ツ都不明白 2025-01-25 07:36:23

也许在匹配阶段的其他方面添加“进度”您仅与单个field ActionTaken匹配,该对象在集合中不存在:

 {
  "$match": {
    "userActivities": {
      "actionTaken": "Sign in.",
      "progress": "Success."
    }
  }
 }

样品Mongo游乐场

Maybe add the "progress" in your match stage otherways you match only objects with the single field actionTaken that do not exist in the collection :

 {
  "$match": {
    "userActivities": {
      "actionTaken": "Sign in.",
      "progress": "Success."
    }
  }
 }

Sample Mongo Playground

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