1

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'

4
  • Use regular dot notation for the query + docs.mongodb.com/manual/reference/operator/projection/… to retrieve only the matching array elements. Commented Jul 13, 2020 at 4:15
  • I already used it not working Commented Jul 13, 2020 at 4:23
  • You want to regex search 'Vi' in both friends and following.friends? You need to user the aggregation pipe where you should first use the $unwind both the array objects and then regex match them and use $regex to match your string then user $project to get the required output. Commented Jul 13, 2020 at 4:35
  • can you help with code I have tried many things Commented Jul 13, 2020 at 6:56

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.