MongoDB: $elemMatch -


i'm trying use $elemmatch find object in array. imported following data collection named trails:

{ "copper" : [         {"name" : "spaulding bowl", "level" : "extreme terain", "location" : "east side"},         {"name" : "resolution bowl", "level" : "double black", "location" : "east side"},         {"name" : "black bear glade", "level" : "double black", "location" : "east side"},         {"name" : "free fall glade", "level" : "double black", "location" : "east side"}  ] } 

i'm using syntax mongodb documentation make following query:

db.trails.find( { "copper": { $elemmatch: { "name" : "spaulding bowl" } }  } ) 

i have tried formating without quotations around keys:

db.trails.find( { copper: { $elemmatch: { name : "spaulding bowl" } }  } ) 

instead of returning 1 matching element, both return entire array of objects. there syntax error i'm missing? tips appreciated.

$elemmatch(query) returns rows in array when there atleast 1 row matching query criteria.

$elemmatch(projection) returns first row of matching rows when used projection.

you don't need elemmatch case single criteria.

db.trails.find({"copper.name": { "spaulding bowl" } }) 

try below uses elemmatch projection variation.

db.trails.find({}, {"copper": { $elemmatch: { "name" : "spaulding bowl" } }  } ) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -