Javascript 不需要聚合项目mongodb输出
我正在用javascript编写一个复杂的查询。 我正在使用聚合。 我用两张桌子。发票、旅行 这是我的密码Javascript 不需要聚合项目mongodb输出,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我正在用javascript编写一个复杂的查询。 我正在使用聚合。 我用两张桌子。发票、旅行 这是我的密码 Invoice.aggregate([ // filter the documents from invoice of only 2016 { $match: { executed: { $gte: startDate, $lte: endDate
Invoice.aggregate([
// filter the documents from invoice of only 2016
{
$match: {
executed: {
$gte: startDate,
$lte: endDate
},
"modelHolder.name": 'Travel'
}
},
// $lookup is working alone, it is not taking the input from function 1 of aggregate
{
$lookup: {
from: "travels",
localField: "modelHolder.id",
foreignField: "_id",
as: "dataFromTravels"
}
},
// filter by date of reservation and date of arrival
$match: { $or:
[
{
'dataFromTravels.from.date': {
$gte: departDate, $lte: endDate
}
},
{
'dataFromTravels.to.date': {
$lte: arrivalDate
},
}
]
}
},
{
$limit: 2
}
// 2nd function to work on the first function output as the input
], function (err, result) {
if (err) {
return console.log(('ERROR', err));
//next(err);
} else {
console.log('Result', result);
// res.json(result);
return;
}
});
// console.log('invoice !');
// console.log(invoice._id);
self.resume();
})
.on('error', function (err) {
console.error('Error occurred while streaming invoices', err);
})
.on('end', function () {
console.log('Successfully displayed invoices');
cb();
});
我想有一个汽车的清单,并计算在比赛功能限制的时间内,我们使用每辆汽车的次数
我加了这个
{
$group: { "_id": "$dataFromTravels.car.plateNumber", "count": { $sum: 1 } }
},
汽车在旅行台下面。
我刚买了3辆车。或者我在同一时期拥有数百辆汽车
我该怎么办
感谢您的建议。当您使用mongodb进行连接查询时。目标集合存储为数组。因此,当您使用查找时
{
$lookup: {
from: "travels",
localField: "modelHolder.id",
foreignField: "_id",
as: "dataFromTravels"
}
}
查询的输出如下:-
{
"_id": ObjectId("554dc5e937c1482c491d9d36"),
// invoice data
"dataFromTravels": [
// travel data
]
}
对于mongodb聚合,您不能对嵌套文档使用$match。
所以,在查询中进行匹配
db.invoice.aggregate([
{
$match: {
executed: {
$gte: ISODate(startDate),
$lte: ISODate(endDate)
},
"modelHolder.name": 'Travel'
}
},
{
$lookup: {
from: "travels",
localField: "modelHolder.id",
foreignField: "_id",
as: "dataFromTravels"
}
}, {
$unwind: "$dataFromTravels"
},{
$match: {
$or: [{
'dataFromTravels.from.date': {
$gte: ISODate(startDate),
$lte: ISODate(endDate)
}
}, {
'dataFromTravels.to.date': {
$lte: ISODate(startDate)
},
}]
}
}, {
$limit: 2
}
]);
现在,dataFromTravels将作为一个对象提供&您可以在from和to属性上使用$match您可以添加输入参数值以及来自集合的示例文档和对帖子的预期json响应吗?how bro?我不明白你的意思,我的意思是你能从
invoice
和travel
集合中添加一些样本文档,以及上述查询的预期响应,你能给我写一个例子吗?我的意思是表invoice
和travel
。你能看到那些表中的数据吗?你能把一些数据复制到邮局吗?因此,人们可以对数据运行您的查询。