使用mongoose和node获取不同的元素

时间:2017-02-20 16:49:34

标签: javascript node.js mongodb mongoose

我在mongo中有数据存储,其中包含名为 _listing 的集合。

{   "_id": "58a1ddcb8850c027298c4dc8",   "name": "Listing 1",  
 "description": "",   "ticket": {
     "amount": 2,
     "type": "electronic",
     "splitType": 2,
     "seating": [
       {
         "category": 1,
         "section": 2,
         "row": 2
       },
       {
        "category": 3,
         "section": "uiui",
         "row": 33
       }
     ]   },   "metadata": {
     "user": {},
     "event": {},
     "venue": {}   },   "listingId": "aabb1cb6-4e79-4d48-9451-e5247230d397" }

我想从上面检索唯一的类别。

这是我的代码。

DBhelper.js

distinct: function (model, conditons, projection, options, distinctKey) {
            return new Promise(function (resolve, reject) {
                options = options || {};
                projection = projection || {};
                model.find(conditons, projection, options).distinct(distinctKey, function (error, data) {
                    if (error)
                        console.log("error is" + err);
                        reject(error);
                    console.log("data is " + data);
                    resolve(data);
                })
            })
        }

我打电话给上面的函数,

dbHelper.distinct(mongoose.model('_listing'), {}, {}, { 'ticket.seating.category': '1' }, 'ticket.seating.category', function (error, data) {
                console.log(data);
                callback(data);
            });

我无法在上述方法中获取数据,而数据在dbhelper函数中打印。问题是什么?

0 个答案:

没有答案