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

502
Views
Cómo comparar dos elementos de objeto en una matriz mongodb
{ "customerSchemes": [ { "name": "A", "startDate": some date in valid date format }, { "name": "B", "startDate": some date in valid date format. } ] }

Estoy tratando de averiguar todos los documentos donde el esquema A comenzó antes que el esquema B. Tenga en cuenta que el esquema Array no está en orden ascendente de startDate. El plan B puede tener una fecha anterior en comparación con el plan A. Creo que el operador de desconexión podría ser útil aquí, pero no estoy seguro de cómo avanzar con los próximos pasos.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede usar la matriz $unwind y formatear los elementos para compararlos transformándolos efectivamente en un par de valores clave. Esto supone que solo tiene dos valores de matriz, por lo que no sabía aplicar ningún filtro.

Algo como

 db.colname.aggregate( [ {"$unwind":"$customerSchemes"}, {"$group":{ "_id":"$_id", "data":{"$push":"$$ROOT"}, "fields":{ "$mergeObjects":{ "$arrayToObject":[[["$customerSchemes.name","$customerSchemes.startDate"]]] } } }}, {"$match":{"$expr":{"$lt":["$fields.A","$fields.B"]}}}, {"$project":{"_id":0,"data":1}} ])

Ejemplo de trabajo aquí: https://mongoplayground.net/p/mSmAXHm0-o-

Usando $reduce

 db.colname.aggregate( [ {"$addFields":{ "fields":{ "$reduce":{ "input":"$customerSchemes", "initialValue":{}, "in":{ "$mergeObjects":[ {"$arrayToObject":[[["$$this.name","$$this.startDate"]]]}, "$$value"] } } } }}, {"$match":{"$expr":{"$lt":["$fields.A","$fields.B"]}}}, {"$project":{"fields":0}} ])

Ejemplo de trabajo aquí: https://mongoplayground.net/p/WNxbScI9N9b

over 4 years ago · Santiago Trujillo Report

0

agregar():

  • $filter para filtrar el name: "A" de customerSchemes
  • $arrayElemAt para obtener el primer elemento del resultado filtrado del paso anterior
  • mismos pasos que arriba para el name: "B"
  • $let declarar variables para "A" en a y "B" en b
  • in verificar la condición de las variables anteriores si la a de startDate de a es mayor que la fecha de inicio de b , luego startDate verdadero, de lo contrario, falso
  • La expresión $expr coincide con $eq para que coincida con el proceso anterior, si es cierto, devuelve el documento
 db.collection.aggregate([ { $match: { $expr: { $eq: [ { $let: { vars: { a: { $arrayElemAt: [ { $filter: { input: "$customerSchemes", cond: { $eq: ["$$this.name", "A"] } } }, 0 ] }, b: { $arrayElemAt: [ { $filter: { input: "$customerSchemes", cond: { $eq: ["$$this.name", "B" ] } } }, 0 ] } }, in: { $gt: ["$$a.startDate", "$$b.startDate"] } } }, true ] } } } ])

Patio de recreo


encontrar():

También puede usar la condición de expresión de la etapa de coincidencia anterior en la consulta find() sin ninguna canalización de agregación,

Patio de recreo

sugerencia de soporte más reciente : si está utilizando la versión más reciente (4.4) de MongoDB, puede usar $first en lugar de $arrayElemAt , consulte Playground

over 4 years ago · Santiago Trujillo Report

0

entonces la idea es

  1. Ordene la matriz customerSchemes por startDate .
  2. Elija el primer elemento de la lista ordenada.
  3. Inclúyalo solo si customerSchemes.name es A .

Prueba esta consulta:

 db.collection.aggregate([ { $unwind: "$customerSchemes" }, { $sort: { "customerSchemes.startDate": 1 } }, { $group: { _id: "$_id", customerSchemes: { $push: "$customerSchemes" } } }, { $match: { $expr: { $eq: [{ $first: "$customerSchemes.name" }, "A"] } } } ]);

Producción:

 /* 1 createdAt:3/12/2021, 6:40:42 PM*/ { "_id" : ObjectId("604b685232a8d433d8ede6c4"), "customerSchemes" : [ { "name" : "A", "startDate" : ISODate("2021-03-01T00:00:00.000+05:30") }, { "name" : "B", "startDate" : ISODate("2021-03-02T00:00:00.000+05:30") } ] }, /* 2 createdAt:3/12/2021, 6:40:42 PM*/ { "_id" : ObjectId("604b685232a8d433d8ede6c6"), "customerSchemes" : [ { "name" : "A", "startDate" : ISODate("2021-03-01T00:00:00.000+05:30") }, { "name" : "B", "startDate" : ISODate("2021-03-05T00:00:00.000+05:30") } ] }

Datos de prueba:

 /* 1 createdAt:3/12/2021, 6:40:42 PM*/ { "_id" : ObjectId("604b685232a8d433d8ede6c4"), "customerSchemes" : [ { "name" : "A", "startDate" : ISODate("2021-03-01T00:00:00.000+05:30") }, { "name" : "B", "startDate" : ISODate("2021-03-02T00:00:00.000+05:30") } ] }, /* 2 createdAt:3/12/2021, 6:40:42 PM*/ { "_id" : ObjectId("604b685232a8d433d8ede6c5"), "customerSchemes" : [ { "name" : "A", "startDate" : ISODate("2021-03-03T00:00:00.000+05:30") }, { "name" : "B", "startDate" : ISODate("2021-03-02T00:00:00.000+05:30") } ] }, /* 3 createdAt:3/12/2021, 6:40:42 PM*/ { "_id" : ObjectId("604b685232a8d433d8ede6c6"), "customerSchemes" : [ { "name" : "B", "startDate" : ISODate("2021-03-05T00:00:00.000+05:30") }, { "name" : "A", "startDate" : ISODate("2021-03-01T00:00:00.000+05:30") } ] }
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!