Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
$filtrar dentro de $reducir o dentro de $mapa de matriz sin desenrollar

Necesito ayuda: quiero optimizar esta consulta para que sea más rápida, necesita filtrar por events.eventType:"log" all docs with server:"strong", pero sin etapas separadas de desenrollado y filtrado, tal vez de alguna manera dentro de $reduce etapa para agregar $ filtro.

ejemplo de documento único:

 { server: "strong", events: [ { eventType: "log", createdAt: "2022-01-23T10:26:11.214Z", visitorInfo: { visitorId: "JohnID" } }

consulta de agregación actual:

 db.collection.aggregate([ { $match: { server: "strong" } }, { $project: { total: { $reduce: { input: "$events", initialValue: { visitor: [], uniquevisitor: [] }, in: { visitor: { $concatArrays: [ "$$value.visitor", [ "$$this.visitorInfo.visitorId" ] ] }, uniquevisitor: { $cond: [ { $in: [ "$$this.visitorInfo.visitorId", "$$value.uniquevisitor" ] }, "$$value.uniquevisitor", { $concatArrays: [ "$$value.uniquevisitor", [ "$$this.visitorInfo.visitorId" ] ] } ] } } } } } } ])

resultado esperado, dos listas con un ID de visitante único y una lista de todos los ID de visitante:

 [ { "total": { "uniquevisitor": [ "JohnID" ], "visitor": [ "JohnID", "JohnID" ] }

} ]

patio de recreo

En la consulta de ejemplo, no se agrega ningún filtro para events.eventType:"log", ¿cómo se puede implementar esto sin $unwind?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No estoy seguro de que este enfoque esté más optimizado que el suyo, pero podría ser útil,

  • $filter para iterar el bucle de events y filtrar por eventType
  • $let declarar events variables y almacenar el resultado de los filtros anteriores
  • devuelve una matriz de visitor usando la notación de puntos $$events.visitorInfo.visitorId
  • devuelve una matriz de visitante único uniquevisitor mediante el uso de la notación de puntos $$events.visitorInfo.visitorId y el operador $setUnion
 db.collection.aggregate([ { $match: { server: "strong" } }, { $project: { total: { $let: { vars: { events: { $filter: { input: "$events", cond: { $eq: ["$$this.eventType", "log"] } } } }, in: { visitor: "$$events.visitorInfo.visitorId", uniquevisitor: { $setUnion: "$$events.visitorInfo.visitorId" } } } } } } ])

Patio de recreo


O un enfoque similar sin $let y dos etapas de $project ,

 db.collection.aggregate([ { $match: { server: "strong" } }, { $project: { events: { $filter: { input: "$events", cond: { $eq: ["$$this.eventType", "log"] } } } } }, { $project: { total: { visitor: "$events.visitorInfo.visitorId", uniquevisitor: { $setUnion: "$events.visitorInfo.visitorId" } } } } ])

Patio de recreo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!