0

Document schema is as follows "events": [ { "title": "title1" }, { "title": "title2" }, { "title": "title3" } ]

I have requirement to search (regex ) in events.title field and get the only matching element/object from the array . For that i am querying like this ,

db.collection.find({ "$or" : [ { "events.title" : { "$regex" : ".*title2.*" , "$options" : "i"}} , { "events.title" : { "$regex" : ".*title5.*" , "$options" : "i"}}] , "events" : { "$elemMatch" : { "title" : { "$ne" :  null }}}},{"events.$":1,"_id":0});

I was expecting the result would be { "events" : [ { "title" : "title2"} ] } , but it returns

{ "events" : [ { "title" : "title1"} ] }

How can i update the query so that only matching element in array is returned in result ?

UPDATE

"events": {
        $elemMatch: {
            "$or": [{
                "title": {
                    "$regex": ".*title2.*",
                    "$options": "i"
                }
            },
            {
                "title": {
                    "$regex": ".*title5.*",
                    "$options": "i"
                }
            }]
        }
    }

did the trick . Now it returns only matching element in the array irrespective of the position.

Thanks for the help

2
  • 1
    The $ means the first element. You can do it with aggregation maybe but you can filter them after you get the result. Commented Nov 1, 2016 at 8:30
  • @AmiramKorach If there is no other solution i can filter the result again using drivers . Commented Nov 1, 2016 at 9:01

2 Answers 2

1

Try this

db.collection.find({events: {
                             $elemMatch: {
                                          title: {$in: [/.*title2.*/i,
                                                        /.*title5.*/i]
                                                 }
                                         }
                            }
                   },
                   {"events.$": 1, _id: 0});
Sign up to request clarification or add additional context in comments.

5 Comments

This will still give him the first item. $ means first.
That's right, it gives him the first item that matches the regex.
@AndriySimonov this still gives the first element . Is there any other way ?
@Mohammedshebin The question said the only matching element/object from the array. The query returns documents with the first matching array element. It looks exactly as you mentioned in the expected result. I assumed you need only one element. Do you actually need all matching array elements?
"events": { $elemMatch: { "$or": [{ "title": { "$regex": ".*title2.*", "$options": "i" } }, { "title": { "$regex": ".*title5.*", "$options": "i" } }] } } This in projection did the trick . Thanks for the help
0
{"events": {
        $elemMatch: {
            "$or": [{
                "title": {
                    "$regex": ".*title2.*",
                    "$options": "i"
                }
            },
            {
                "title": {
                    "$regex": ".*title5.*",
                    "$options": "i"
                }
            }]
        }
    }
}

in projection did the trick . Now it returns only matching element in the array irrespective of the position

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.