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

97
Views
throw an error when $match doesn't find any matching doc

I have the following code:

const userSchema = new mongoose.Schema({
   name: String,
   loginAttempts: [ { IP: String } ]
})
const User = mongoose.model('User', userSchema, 'users')
app.get('/users/:userId/login-attempts', async(req,res,next)=>{
    const loginAttempt = await User.aggregate([
        { $match: { _id: mongoose.Types.ObjectId(req.params.userId) } },
        { $unwind: '$loginAttempts' },
        { $replaceRoot: { newRoot: '$loginAttempts' } },
    ])
})

I want when the operator $match does not find a matching document to throw an error to report something at least, to show the client a real message that the req.params.id he's looking for does not exist in the first stage.

But how?

about 4 years ago · Santiago Trujillo
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!