collection.aggregate(...)。cursor不是函数loopback mongodb
我遇到过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);
});
回答:
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);
});
});
以上是 collection.aggregate(...)。cursor不是函数loopback mongodb 的全部内容, 来源链接: utcz.com/qa/264938.html