我在loopback v3.8.0中遇到了 TypeError:collection.aggregate(...)。cursor不是函数,loopback mongodb connector v1.18.1。
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 :(得分: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);
});
});