Estoy tratando de realizar una búsqueda que funciona bien y en el documento correcto como 'métricas'. El documento de búsqueda tiene una matriz dentro de su objeto llamada 'historial'. Estoy tratando de deshacer ese historial y realizar una faceta en él, una consulta de agregación que tengo directamente en la colección de búsqueda que funciona bien.
Sin embargo, cuando lo usa aquí, no devuelve nada. ¿Estoy desenrollando esto incorrectamente? ¿Debería ser $metrics.history ?
{ from: 'historicprices', localField: 'collectibleId', foreignField: 'collectibleId', pipeline: [ {$set: {"target-date": "$$NOW"}}, {$unwind: {path: "$history"}}, {$facet: { "one_day": [ { $match: { $expr: { $lte: [{$subtract: ["$target-date", "$history.date" ]}, {$multiply: [24,60,60,1000] }] } } }, { $group: { _id: null, "first": { $first: "$history.value" }, "last": { $last: "$history.value" }, "min-price": {"$min": "$history.value"}, "max-price": {"$max": "$history.value"} } }, { $unset: ["_id"]} ] "one_week": [ { $match: { $expr: { $lte: [{$subtract: ["$target-date", "$history.date" ]}, {$multiply: [7, 24, 60, 60, 1000] }] } } }, { $group: { _id: null, "min-price": {"$min": "$history.value"}, "first": { $first: "$history.value" }, "last": { $last: "$history.value" }, "max-price": {"$max": "$history.value"} } }, { $unset: ["_id"]} ] }} ], as: 'metrics', }Gracias
Tal vez necesite agregar preserveNullAndEmptyArrays: true a su etapa de relajación. De lo contrario, no se recibirán datos si uno de los documentos no devuelve este campo.