Home > Net >  Query within a object with variable keys?
Query within a object with variable keys?

Time:10-18

I have an object and I want to query if this term exists within the object and if yes return it. I don't know the term which will be used to query. Let's assume I queried answer1 then return it

response:{
 "101": "answer1",
  "201": "answer2",
  "301": "answer3",
  "100": "answer4",
  "200": "answer5",
  "300": "answer6",
  "111": "answer7",
  "211": "answer8",
  "311": "answer9"
}

TIA

CodePudding user response:

Try this way:

db.collection.createIndex( { '$**' : "text" } )
db.collection.aggregate([{$match : { $text: { "$search": "answer1" } }}])

Here we are creating the indexes with text and then doing a text search.

Hope this helps you!

  • Related