2017-12-27 163 views
0

我遇到过TypeError:collection.aggregate(...)。cursor不是函数 in loopback v3.8.0,loopback mongodb connector v1 .18.1。collection.aggregate(...)。cursor不是函数loopback mongodb

var pipeline = [{ 
     $match: { 
      restaurantId: id 
     } 
     }, { 
     $project: { 
      'y': { 
      '$year': '$orderDateTime' 
      }, 
      'm': { 
      '$month': '$orderDateTime' 
      }, 
      'd': { 
      '$dayOfMonth': '$orderDateTime' 
      } 
     } 
     }, { 
     $group: { 
      '_id': { 
      'year': '$y', 
      'month': '$m', 
      'day': '$d' 
      }, 
      'sum': { 
      '$sum': '$totalAmount' 
      } 
     } 
     }]; 

     Model.getDataSource().connector.connect(function(err, db) { 
     var collection = db.collection('collection-name'); 
     collection.aggregate(pipeline).cursor({ batchSize: 2500, async: true }).exec(); 
     }); 

取而代之的模型名称,我也尝试通过以下提供集合名称。

var sampleCollection = SampleModel.getDataSource().connector.collection(SampleModel.modelName); 
var data = sampleCollection.aggregate(pipeline).cursor({ batchSize: 1000, async: true }).exec(function(err, cursor){ 
     console.log(cursor); 
}); 
+0

我记得'aggregate'返回一个数组,'cursor'正常操作('find') – Khang

+0

@Khang但总回报 'AggregationCursor' 不是一个 '阵列'。我怀疑这个错误是否与驱动程序本身有关。请参阅https://github.com/Automattic/mongoose/issues/2306 –

回答

0
var pipeline = [{ 
     $match: { 
      restaurantId: ObjectID(id) 
     } 
     }, { 
     $project: { 
      'y': { 
      '$year': '$orderDateTime' 
      }, 
      'm': { 
      '$month': '$orderDateTime' 
      }, 
      'd': { 
      '$dayOfMonth': '$orderDateTime' 
      } 
     } 
     }, { 
     $group: { 
      _id: { 
      year: '$y', 
      month: '$m', 
      day: '$d' 
      }, 
      totalRevenue: { 
      '$sum': '$totalBillAmount' 
      } 
     } 
     }]; 
     Model.getDataSource().connector.connect(function(err, db) { 
     var collection = db.collection('model-name'); 
     var cursor = collection.aggregate(pipeline, function(err, results) { 
      console.log(results); 
     }); 
     });