Getting a list of unique embedded/nested objects in a MongoDB document

前端 未结 4 1511
予麋鹿
予麋鹿 2020-12-16 01:24

Consider the following MongoDB \"recipes\" collection:

{
  \"title\" : \"Macaroni and Cheese\",
  \"ingredients\" : [
    { \"name\" : \"noodles\", \"qty\" :         


        
4条回答
  •  太阳男子
    2020-12-16 01:35

    Use distinct to find an array of distinct values for ingredients.name

    db.recipes.distinct('ingredients.name')
    

    yields [ "butter", "cheese", "noodles", "flour", "sugar", "water" ]

提交回复
热议问题