2

I have a data where an array is there. Under that array Many array of objects is there. I am mentioning the raw data so that anyone guess the structure

{
 _id: ObjectId(dfs45sd54fgds4gsd54gs5),
 content: [
 {
  str: "Hey",
  isDelete: false 
 },
 {
  str: "world",
  isDelete: true
 }
]
}

So I want to search any string that match and I have top search under an array.

So my query is like this:

let searchTerm = req.body.key;
db.collection.find(
 {
  'content.str': {
    $regex: `.*\\b${searchTerm}\\b.*`,
    $options: 'i',
   }
 }
)

So this will return the data. Now for some reason I have to search the data if isDelete: false.

Right now it returns the data whether isDelete is true/false because I have not mentioned the conditon.

Can anyone help me out regarding this to get the data through condition. I want this to Mongodb Query only.

Any help is really appreciated.

4
  • You can use lookahead assertion in regex. Example - (world).*\s+(?=isDelete: false). So first regex looks for world and when it finds it then it starts looking for isDelete: false and then it returns a match only if condition satisfies or else it wont result in a match Commented Jul 31, 2020 at 16:21
  • @rootkonda thanks for your response, I tried this but not works Commented Jul 31, 2020 at 16:38
  • Can you paste here what you tried ? Commented Jul 31, 2020 at 16:41
  • let searchTerm = ${req.body.key}.*\s+(?=isDelete: false); db.collection.find( { 'content.str': { $regex: .*\\b${searchTerm}\\b.*, $options: 'i', } } ) Commented Jul 31, 2020 at 17:42

2 Answers 2

1

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria,

db.collection.find({
  content: {
    $elemMatch: {
      isDelete: true,
      str: {
        $regex: `.*\\b${searchTerm}\\b.*`,
        $options: "i"
      }
    }
  }
},
{
  "content.$": 1
})

Working Playground: https://mongoplayground.net/p/VkdWMnYtGA3

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

4 Comments

@Aks Is there something in the provided answer that you believe does not address your question? If so then please comment on the answer to clarify what exactly needs to be addressed that has not.
Hey there, your code is working but it fetches only 1 object, but I want it to find all matches data as a search result.
@Tushar you can use $filter operator in projection, see the example playground
it's working. thanks for giving me your time. I think you have deep knowledge of MongoDB. great
0

You can add another condition there as belo

db.test2.find({
  $and: [
    {
      "content.str": {
        $regex: "hey",
        $options: "i",
        
      }
    },
    {
      "content.isDelete": false
    }
  ]
},
{
    'content.$':1 //Projection - to get only matching array element
})

11 Comments

I tried this but it not working because this search is running under array. If it like straight entry (Without nested structure) then It works. I tried this
Could you explain what do you mean by straight entry?
Like normal key and value (Normal JSON Structure). Anyways I again tried this but this will return the entry whose isDelete: true value which I don't want on response as I assume that this entry is remove from DB
@Gibbs I'm excited, how does this positional operator works in projection?
I tried but its like same case What i mentioned on question. I return the main _id on projection. So if that keyword is deleted then response return the empty array as not data found on Showing on Frontend. I tried your and condition and projection also but not works. I am also surprised why this things not happend
|

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.