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

257
Views
Recuento de MongoDB por mes actual

Soy nuevo en MongoDB. Necesito obtener el recuento de las publicaciones que se publican en este mes actual. PlateModel es mi modelo. También he mantenido las marcas de tiempo verdaderas. Se parece a lo siguiente:

 { timestamps: true }

Mi código actual en el controlador se ve a continuación:

 const today = new Date(); const host_count = await PlateModel.find({ $and: [ { postedBy: req.user._id, }, { createdAt: today.getMonth(), }, ], }).count();

Pero obtengo el valor de conteo 0. ¿Alguien puede ayudarme a resolver el problema?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede filtrar los objetos desde el comienzo del mes actual hasta el final del próximo mes:

 const startOfCurrentMonth = new Date(); startOfCurrentMonth.setDate(1); const startOfNextMonth = new Date(); startOfNextMonth.setDate(1); startOfNextMonth.setMonth(startOfNextMonth.getMonth() + 1); const host_count = await PlateModel.find({ $and: [ { postedBy: req.user._id, }, { createdAt: { $gte: startOfCurrentMonth, $lt: startOfNextMonth } }, ], }).count();
over 4 years ago · Santiago Trujillo Report

0

Puede utilizar la biblioteca Moment .

 const moment = require("moment"); const firstdate = moment().startOf('month').format('DD-MM-YYYY'); console.log(firstdate); const lastdate=moment().endOf('month').format("DD-MM-YYYY"); console.log(lastdate); const host_count = await PlateModel.find({ $and: [ { postedBy: req.user._id, }, { createdAt: { $gte:firstdate, $lt:lastdate } }, ], }).count();
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!