MongoDB: How to get distinct list of sub-document field values?

后端 未结 2 2075
时光取名叫无心
时光取名叫无心 2020-12-08 10:04

Let\'s say I have the following documents in collection:

{
   \"family\": \"Smith\",
   \"children\": [
        {
            \"child_name\": \"John\"
               


        
2条回答
  •  没有蜡笔的小新
    2020-12-08 10:35

    You can just do:

    db.collection.distinct("children.child_name");
    

    In your case it returns:

    [ "John", "Anna", "Kevin" ]
    

提交回复
热议问题