MongoDB: $elemMatch

后端 未结 1 1062
被撕碎了的回忆
被撕碎了的回忆 2021-01-26 22:29

I\'m trying to use $elemMatch to find an object in an array. I imported the following data into a collection named trails:

{ \"Copper\"         


        
相关标签:
1条回答
  • 2021-01-26 23:13

    $elemmatch(query) returns all rows in a array when there is atleast one row matching the query criteria.

    $elemMatch(projection) returns only the first row of all the matching rows when used as projection.

    You don't need elemMatch for your case as it is only single criteria.

    db.trails.find({"Copper.name": { "Spaulding Bowl" } })
    

    Try as below which uses the elemMatch projection variation.

    db.trails.find({}, {"Copper": { $elemMatch: { "name" : "Spaulding Bowl" } }  } )
    
    0 讨论(0)
提交回复
热议问题