Prateek Sharma
Prateek Sharma

Reputation: 39

How to get only queried array object from an array in mongo db with regex?

I have my data saved in mongo db like this

  {
 "_id": "5eff4376e036e45de9dbc6df",
 "social_connections":{
     "friend":{
         "friends":[
            {
                "_id": "5eff42dee036e45de9dbc6d3",
                "user_name": "x",
                "name": "Viper King"
            },
            {
                "_id": "5eff40efe036e45de9dbc6c9",
                "user_name": "z",
                "name": "Brad Prasad Pitt"
            },
            {
                "_id": "5eff50337508da5ff40bf36e",
                "user_name": "test",
                "name": "Test"
            }

         ]
         
     },
     "followers":{
        "following":[
            {
                "_id": "5eff42dee036e45de9dbc6d3",
                "user_name": "x",
                "name": "Viper King"
            },
            {
                "_id": "5eff40efe036e45de9dbc6c9",
                "user_name": "z",
                "name": "Brad Prasad Pitt"
            },
            {
                "_id": "5eff50337508da5ff40bf36e",
                "user_name": "test",
                "name": "Test"
            }
        ]
     }
 }
 }

I want to make a mongo db query in which I would pass the _id of the document that is 5eff4376e036e45de9dbc6df and a regex that is 'Vi', now I want all those array objects whose name contains 'Vi'. My expected output is:-

  {
 "_id": "5eff4376e036e45de9dbc6df",
 "social_connections":{
     "friend":{
         "friends":[
            {
                "_id": "5eff42dee036e45de9dbc6d3",
                "user_name": "x",
                "name": "Viper King"
            }

         ]
         
     },
     "followers":{
        "following":[
            {
                "_id": "5eff42dee036e45de9dbc6d3",
                "user_name": "x",
                "name": "Viper King"
            }
        ]
     }
 }
 }

You can also make a query which only return _id of those whose name contains 'Vi'

Upvotes: 1

Views: 1306

Answers (1)

mickl
mickl

Reputation: 49985

You can use $filter to get a new filtered array and $regexMatch to apply your regular expression:

db.collection.aggregate([
    {
        $project: {
            "social_connections.friend.friends": {
                $filter: {
                    input: "$social_connections.friend.friends",
                    as: "friend",
                    cond: {
                        $regexMatch: { input: "$$friend.name", regex: "Vi" }
                    }
                }
            },
            "social_connections.followers.following": {
                $filter: {
                    input: "$social_connections.followers.following",
                    as: "follower",
                    cond: {
                        $regexMatch: { input: "$$follower.name", regex: "Vi" }
                    }
                }
            }
        }
    }
])

Mongo Playground

Upvotes: 2

Related Questions