Home > Blockchain >  Nested array aggregate only taking one field to match
Nested array aggregate only taking one field to match

Time:04-01

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?

CodePudding user response:

Use dot notation in your $match stage.

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

Sample Mongo Playground

CodePudding user response:

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
      }
    ]

CodePudding user response:

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

  • Related