Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested array aggregate only taking one field to match

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Use dot notation in your $match stage.

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

Sample Mongo Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading