Node.js 如何对多个集合执行Mongodb聚合筛选?
我有两个收款预订和发票,我已经为以下条件准备了汇总和查找查询 预订收集条件 条件1:状态不等于已交付 条件2:产品不应为空Node.js 如何对多个集合执行Mongodb聚合筛选?,node.js,mongodb,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongodb Query,Aggregation Framework,我有两个收款预订和发票,我已经为以下条件准备了汇总和查找查询 预订收集条件 条件1:状态不等于已交付 条件2:产品不应为空 db.bookings.aggregate([ { "$match": { "Status": { $ne: "Delivered" } } }, { "$lookup": { "from"
db.bookings.aggregate([
{
"$match": {
"Status": {
$ne: "Delivered"
}
}
},
{
"$lookup": {
"from": "invoices",
"localField": "Invoices",
"foreignField": "_id",
"as": "invoiceInfo"
}
},
{
"$match": {
"$or": [
{
"BookingData.products": {
"$exists": true
}
},
{
"invoiceInfo.InvoiceData": {
"$exists": true
}
}
]
}
},
{
$set: {
"BookingData.products": {
"$filter": {
"input": "$BookingData.products",
"cond": {
$and: [
{ $ne: [ "$$this.ProductID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsDeliveryFailed", "Yes" ] }
]
}
}
}
}
},
{
$set: {
"invoiceInfo.InvoiceData": {
"$filter": {
"input": "$invoiceInfo.InvoiceData",
"cond": {
$and: [
{ $ne: [ "$$this.InvoiceID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsPaymentFailed", "Yes" ] }
]
}
}
}
}
},
{
$match: {
$expr: {
$or: [
{
$ne: [
"$BookingData.products",
[],
]
},
{
$ne: [
"$invoiceInfo.InvoiceData",
[],
]
}
]
}
}
}
])
条件3:ProductID应存在于products数组中,且不应为null
条件4:IsDeliveryFailed不应为“是”
预订收集数据
发票收款条件
条件1:InvoiceData不应为null或空
db.bookings.aggregate([
{
"$match": {
"Status": {
$ne: "Delivered"
}
}
},
{
"$lookup": {
"from": "invoices",
"localField": "Invoices",
"foreignField": "_id",
"as": "invoiceInfo"
}
},
{
"$match": {
"$or": [
{
"BookingData.products": {
"$exists": true
}
},
{
"invoiceInfo.InvoiceData": {
"$exists": true
}
}
]
}
},
{
$set: {
"BookingData.products": {
"$filter": {
"input": "$BookingData.products",
"cond": {
$and: [
{ $ne: [ "$$this.ProductID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsDeliveryFailed", "Yes" ] }
]
}
}
}
}
},
{
$set: {
"invoiceInfo.InvoiceData": {
"$filter": {
"input": "$invoiceInfo.InvoiceData",
"cond": {
$and: [
{ $ne: [ "$$this.InvoiceID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsPaymentFailed", "Yes" ] }
]
}
}
}
}
},
{
$match: {
$expr: {
$or: [
{
$ne: [
"$BookingData.products",
[],
]
},
{
$ne: [
"$invoiceInfo.InvoiceData",
[],
]
}
]
}
}
}
])
条件2:InvoiceID应存在于InvoiceData数组中,且不应为null
条件3:iPaymentFailed不应为“是”
发票收集数据
查询
db.bookings.aggregate([
{
"$match": {
"Status": {
$ne: "Delivered"
}
}
},
{
"$lookup": {
"from": "invoices",
"localField": "Invoices",
"foreignField": "_id",
"as": "invoiceInfo"
}
},
{
"$match": {
"$or": [
{
"BookingData.products": {
"$exists": true
}
},
{
"invoiceInfo.InvoiceData": {
"$exists": true
}
}
]
}
},
{
$set: {
"BookingData.products": {
"$filter": {
"input": "$BookingData.products",
"cond": {
$and: [
{ $ne: [ "$$this.ProductID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsDeliveryFailed", "Yes" ] }
]
}
}
}
}
},
{
$set: {
"invoiceInfo.InvoiceData": {
"$filter": {
"input": "$invoiceInfo.InvoiceData",
"cond": {
$and: [
{ $ne: [ "$$this.InvoiceID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsPaymentFailed", "Yes" ] }
]
}
}
}
}
},
{
$match: {
$expr: {
$or: [
{
$ne: [
"$BookingData.products",
[],
]
},
{
$ne: [
"$invoiceInfo.InvoiceData",
[],
]
}
]
}
}
}
])
这不符合预期,例如,如果
查询应返回上述文档
如果ProductID存在,并且产品存在,并且所有产品都没有IsDeliveryFailed:“是”
如果ProductID存在,并且产品存在,并且任何产品都没有IsDeliveryFailed:“是”
如果InvoiceID存在且InvoiceData存在且所有InvoiceData没有iPaymentFailed:“是”
如果InvoiceID存在且InvoiceData存在且任何InvoiceData没有iPaymentFailed:“是”
另一套
如果ProductID存在且products存在且所有产品均为IsDeliveryFailed:“是”标志。但我们必须检查发票收款情况
如果InvoiceID存在,InvoiceData存在,并且任何InvoiceData没有iPaymentFailed:“是”,则我们必须返回此文档
如果InvoiceID存在且InvoiceData存在且所有InvoiceData均为IsPaymentFailed:“是”。但我们必须检查预订集合
如果ProductID存在,并且产品存在,并且任何产品都没有交付失败:“是”,那么我们必须返回文档
我已在中解释了您以前的问题。由于invoiceInfo是一个数组,
invoiceData
也是invoiceInfo
中的一个数组,因此我们使用map和filter。然后我们需要排除invoiceData
的空数组。(这可以在前面的步骤中完成,也可以像filter map->filter,但可能会很长,这就是我在下一阶段使用它的原因)
这是密码
db.bookings.aggregate([
{
"$match": {
"PaymentStatus": { $ne: "Delivered" }
}
},
{
$set: {
"BookingData.products": {
"$filter": {
"input": "$BookingData.products",
"cond": {
$and: [
{ $ne: [ "$$this.ProductID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsDeliveryFailed", "Yes" ] }
]
}
}
}
}
},
{
"$lookup": {
"from": "invoices",
"localField": "Invoices",
"foreignField": "_id",
"as": "invoiceInfo"
}
},
{
$set: {
invoiceInfo: {
$map: {
input: "$invoiceInfo",
as: "info",
in: {
InvoiceData: {
$filter: {
input: "$$info.InvoiceData",
as: "data",
"cond": {
$and: [
{ $ne: [ "$$data.InvoiceID", undefined ] },
{ $ne: [ "$$data.InvoiceID", null ] },
{ $ne: [ "$$data.IsPaymentFailed", "Yes" ] }
]
}
}
}
}
}
}
}
},
{
$set: {
invoiceInfo: {
$filter: {
input: "$invoiceInfo",
cond: { $ne: [ "$$this.InvoiceData", [] ] }
}
}
}
},
{
$match: {
$expr: {
$or: [
{ $ne: [ "$BookingData.products", [] ] },
{ $ne: [ "$invoiceInfo", [] ] }
]
}
}
}
])
工作
希望这对你有帮助。这是您需要根据您的需求进行游戏/解决的时间。有时您需要在演示中的当前位置之前或之后进行查找ur
invoiceInfo
是数组吗?或者总是有一个元素?invoiceInfo将是一个数组