Saturday 20 May 2017

how to find things in complex structure in mongodb?

lets say i have data in mongodb like

`{
   _id: ObjectId("5234cc89687ea597eabee675"),
   code: "xyz",
   tags: [ "school", "book", "bag", "headphone", "appliance" ],
   qty: [
          { size: "S", num: 10, color: "blue" },
      { size: "M", num: 45, color: "blue" },
      { size: "L", num: 100, color: "green" }
    ]
}

{
_id: ObjectId("5234cc8a687ea597eabee676"),
code: "abc",
tags: [ "appliance", "school", "book" ],
qty: [
      { size: "6", num: 100, color: "green" },
      { size: "6", num: 50, color: "blue" },
      { size: "8", num: 100, color: "brown" }
    ]
}

{
_id: ObjectId("5234ccb7687ea597eabee677"),
code: "efg",
tags: [ "school", "book" ],
qty: [
      { size: "S", num: 10, color: "blue" },
      { size: "M", num: 100, color: "blue" },
      { size: "L", num: 100, color: "green" }
    ]
}

{
_id: ObjectId("52350353b2eff1353b349de9"),
code: "ijk",
tags: [ "electronics", "school" ],
qty: [
      { size: "M", num: 100, color: "green" }
    ]

}

now I want to search form document which size is S and color is blue.

means I want to search in the nested array--object. I see $all and many other $ but exactly I am not understanding how to find like this?



via suresh pareek

No comments:

Post a Comment