He explicado su problema anterior en Pregunta
. Dado que la información de factura es una matriz y invoiceData
también una matriz dentro de invoiceInfo
, usamos mapa y filtro. Luego, debemos excluir la matriz vacía de invoiceData
. (Esto se puede hacer en el paso anterior también como filter-map->filter, pero podría llevar mucho tiempo, por eso lo usé en la siguiente etapa)
Aquí está el código
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", [] ] }
]
}
}
}
])
Trabajando Zona de juegos de Mongo
Espero que esto te ayudará. Este es el tiempo que necesita para jugar/solucionar según sus requisitos. A veces es necesario buscar antes o después de la ubicación actual en la demostración