mongoose:查找数组的唯一类别

时间:2015-10-09 18:21:49

标签: javascript arrays json node.js mongodb

下面的

是“文件”的一个例子,代表mongodb中的商店产品。我想知道从节点api检索类别作为数组的最有效方法是什么?例如,我想要[“女装”,“男士服装”]的结果。我真的不想循环遍历数据库中的所有记录,或者有没有办法可以直接查询类别属性。

 [{
            "name":"eu sit voluptate",
            "description":"Deserunt magna minim in nisi ad nisi amet. Cillum in aliquip ex fugiat ut nisi Lorem amet laboris magna occaecat tempor. Nostrud quis nisi minim aute consectetur quis consectetur in nulla. Laboris velit sunt et adipisicing duis consequat do.\r\n",
            "color":"neutral",
            "price":751.12,
            "category":"womens clothing",
            "subcategory":"tops",
            "id":1,
            "sizes":"XL",
            "images":[
               "https://placeholdit.imgix.net/~text?txtsize=15&txt=320%C3%97320&w=320&h=320"
            ],
            "brand":"paul_smith",
            "created":"2015-01-01T12:53:31 -00:00",
            "inStock":true,
            "sale":[
               {
                  "onSale":false,
                  "salePercOff":10
               }
            ]
         },{
            "name":"sit proident mollit",
            "description":"Occaecat proident adipisicing cillum Lorem in. Nulla ipsum culpa eu proident pariatur culpa. Eiusmod consequat qui ex deserunt sunt ut ullamco labore minim proident sint ipsum officia.\r\n",
            "color":"neutral",
            "price":626.6,
            "category":"mens clothing",
            "subcategory":"jumpers",
            "id":15,
            "sizes":40,
            "images":[
               "https://placeholdit.imgix.net/~text?txtsize=15&txt=320%C3%97320&w=320&h=320"
            ],
            "brand":"james_reed",
            "created":"2014-12-18T03:50:19 -00:00",
            "inStock":true,
            "sale":[
               {
                  "onSale":true,
                  "salePercOff":10
               }
            ]
         }]

1 个答案:

答案 0 :(得分:0)

答案是一个简单的查询:

db.shop.distinct('category');