0

I was wondering how I can query from an embedded document inside an array. I have following structure:

{ "targetId" : 2, "metaData" : [ {     "key" : "id",     "value" : 1 },     {     "key" :        "name",     "value" : "Parisa" },     {     "key" : "img",     "value" : {     "imgid" : 1,         "imgName" : "img1" } } ] 

I could search simple key-values like key = id and value =1, but I could not search based on the values with embedded document e.g. key="img"

I tried following query but it does not work:

db.test.find({"metaData":{$elemMatch:{"key":"img", "value":{"imgid":1}}}})

Could you please help me!

1 Answer 1

1

I think the "value" part of your query is a little off. You need to put the document element in the criteria:

b.test.find({"metaData":{$elemMatch:{"key":"img", "value.imgid":1}}})
Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately, it does not return any document!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.