3

I have the following structure :

{
   "FiledA" : "FiledAValue",
   "FiledB" : "FiledBValue",
   "FiledC" : {
          "X" : "XValue",
          "Y" : "YValue",
          "Z" : "ZValue"
   },
}
  • FiledC content may be dynamic (other fileds than x,y,z)
  • The user will send a query like {"FiledA" : "12" , "FiledC" : "333"} The query should match 12 for FiledA and if there any FiledC which one of its filds contains 333

How I can solve the FiledC issue in a query ?

Thanks in advance ...

Rahul
  • 15,979
  • 4
  • 42
  • 63
Wasim
  • 1,915
  • 5
  • 24
  • 39
  • 1
    You don't want to have dynamic field's name in your documents. It's hard to work with. You should consider to change your documents structure and make `FieldC` an array of sub-documents. – styvane Mar 28 '16 at 11:33

1 Answers1

5

You should not use dynamic fields to query. It is not possible to query on these fields without individually inspecting all the fields. The similar approach has been exemplified at this answer

If you are open to changing your schema, I would recommend changing it to :

{
   FieldC:[
      {name:"X", value:"value1"},
      {name:"Y", value:"value2"},
      {name:"Z", value:"value3"}
   ]
}

You can now query using :

db.collection.find({"FieldC.value":"testValue"});
Community
  • 1
  • 1
Rahul
  • 15,979
  • 4
  • 42
  • 63