嵌套数组聚合只取一个字段匹配

Nested array aggregate only taking one field to match

我在 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 字段,那么它会给我数据。

当要查询的列中有多个字段时,是否应该做一些不同的事情?

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

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

Sample Mongo Playground

也许在你的匹配阶段添加“进度”,否则你只匹配集合中不存在的具有单个字段 actionTaken 的对象:

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

Sample Mongo Playground

通过一个人的帮助得到了答案。我不得不按如下方式使用 $elemMatch。

    [
      {
        "$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
      }
    ]