0

this is how my collection look like

{
    "field1": "aa",
    "field2": [
        {"field21": "bb"},
        {"field22": "cc"},
        {"field23": "dd"},
    ],
    "field3": "ee"
}
{
    "field1": "ff",
    "field2": [
        {"field21": "gg"},
        {"field22": "hh"},
        {"field23": "ii"},
    ],
    "field3": "jj"
}
{
    "field1": "kk",
    "field2": [
        {"field21": "aa"},
        {"field22": "mm"},
        {"field23": "nn"},
    ],
    "field3": "oo"
}

what i want is find the document that the value contains "aa", so the document will be shown is first doc and third doc.

  • Possible duplicate of [MongoDB Query Help - query on values of any key in a sub-object](http://stackoverflow.com/questions/19802502/mongodb-query-help-query-on-values-of-any-key-in-a-sub-object) – Shanoor Feb 11 '16 at 08:47
  • Either change the schema or you can use the (very expensive) `$where`: http://stackoverflow.com/questions/19802502/mongodb-query-help-query-on-values-of-any-key-in-a-sub-object/19802670#19802670 – Shanoor Feb 11 '16 at 08:48
  • Is the 'a' you are looking for always going to be in either field1 or field2.field21? – gyamana Feb 11 '16 at 10:52
  • sorry for my question, maybe it's confusing, i think the one that i need is $text and $search query. that query solved my problem. thanks all – Muhammad Fasalir Rahman Feb 15 '16 at 09:18

1 Answers1

0

i have solved this by using $text $search query

model.find({ $text: { $search: "aa" } }).exec(function(err, docs){
//
}