0

Essentially, I want to query mongo return documents that contain matching keys from a partially defined json object.

Example document:

{ 
    "description" : { 
        "body" : { 
            "type" : "Ectomorph", 
            "shape" : "Spoon", 
            "muscle tone" : "Moderately Tone" 
        } 
    }
}

How I'm attempting to query

db.taxonomy.find({ description: { $elemMatch: { body: { type: "Ectomorph" } } } })

Although, according to the mongo documentation $elemMatch appears to be the operator i want to use, it doesn't appear to be working this way.

Note

I'm aware that the query would work if it was writting as { "decription.body.type": "Ectomorph" }

But the key values I'm searching by are coming from a request and i'd rather not waste processing power parsing the JSON into a query like this if i don't have to.

Dave Maison
  • 383
  • 1
  • 13
  • 1
    `$elemMatch` operator works on arrays, not ordinary objects. see https://docs.mongodb.org/manual/reference/operator/query/elemMatch/ – Saleem Mar 28 '16 at 22:30
  • $elemMatch is actually for arrays. I also didn't really understand how making a query like: { description: { $elemMatch: { body: { type: "Ectomorph" } } } } differs from { "decription.body.type": "Ectomorph" } in terms of processing power. Could you provide an example of how the request looks like? – iagowp Mar 28 '16 at 22:32
  • processing power in that i would have to manually rewrite the object. anyway the request looks the same as the example: { body: { type: "Ectomorph" } } – Dave Maison Mar 28 '16 at 23:49

0 Answers0