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

221
Views
Coincidencia de MongoDB si existe entrada (agregación)
$match: { date: { $gte: fromDate, $lte: toDate }, if (userId) { user: userId } },

Estoy tratando de hacer coincidir un usuario solo si hay un userId de usuario enviado. Cuál es la mejor manera de hacer esto.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede usar el generador de consultas mongoose para administrar las etapas de agregación según sus condiciones,

 // INITIALIZE let p = YourSchemaModel.aggregate(); // DATE FILTER p.match({ date: { $gte: fromDate, $lte: toDate } }); // USER ID FILTER if (userId) p.match({ user: userId }); // RESULT let result = await p.exec();

¡La agregación fusionará automáticamente las etapas $match si ambas están juntas!


Segunda opción:

 let result = await YourSchemaModel.aggregate().match( Object.assign( { date: { $gte: fromDate, $lte: toDate } }, userId ? { user: userId } : {} ) ).exec();
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!