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

358
Views
Mongo findById() only works sometimes even when passed a valid ID

I am having a strange issue querying a Mongo DB collection. I am using findById() to get a single item that works sometimes and not others.

I have checked the id being passed to the server route and in all cases, they match perfectly with the targeted document in the collection.

Here is the basic code:

router.get("/:postId", async (req, res) => {
  console.log('id : ', req.params.postId)
  console.log('type: ', typeof(req.params.postId)) // id is a string
  try {
    const post = await Post.findById(req.params.postId).exec();
    console.log('post :', post) // sometimes null
    res.json(post);
  } catch (err) {
    res.json({ message: err });
  }
});

In the above route, only certain posts will be found while others come back null. This happens regardless of whether the id passed is correct and the document exists with the exact id.

If anyone has any ideas about what could be going wrong here I'd much appreciate the help!

EDIT

I have done some more debugging and think it is something to do with the Schema for the Post model.

For example, this object will be found:

{
"tags": ["foo"],
"_id": "8394839483fhg020834903",
"title": "bar",
"content": "baz",
"isPrivate": true,
}

But this one will not because of the missing isPrivate property.

{
"tags": [],
"_id": "5e0fdc631ef5c46b285a4734",
"title": "New post",
"content": "Some content here",
}

I have tested this across multiple queries and it appears to the root of the problem.

I have tried adding

isPrivate: {
    required: false
  }

To the Schema but it doesn't seem to solve the issue.

Here is the full Schema

const postSchema = mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  content: {
    type: String,
    required: true
  },
  tags: [{ type: String }],
  date: {
    type: Date,
    default: Date.now
  },
  isPrivate: {
    type: Boolean
    required: false
  }
});

I'm not a Mongo/Mongoose expert, so any guidance would be much appreciated.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If post id match with any record it return data, otherwise it will return null. You should handle the exception

router.get("/:postId", async (req, res) => {
   try {
     const post = await Post.findById(req.params.postId).exec();
     if(post) {
       return res.json(post);
     }
     res.json({ message:'No Post found' });
   } catch (err) {
     res.json({ message: err });
   }
});

You can manually check is record exists against a post id. You can use MongoDB Compass for gui browse the record

about 4 years ago · Juan Pablo Isaza Report

0

I believe the issue might be with your _id as per mongo standard _id should be a String is of 12 bytes or a string of 24 hex characters.

We can check if the _id is valid using mongoose.isValidObjectId()

I did run this check on your objects that you posted and indeed 1 is invalid while other is valid

const mongoose = require('mongoose');

console.log(`is '8394839483fhg020834903' valid - ${mongoose.isValidObjectId('8394839483fhg020834903')}`);
console.log(`is '5e0fdc631ef5c46b285a4734' valid - ${mongoose.isValidObjectId('5e0fdc631ef5c46b285a4734')}`);

It gives meValid/Invalid

You will have to check what is modifying your ID's in the code, you can upload your schema to get a better understanding as well.

about 4 years ago · Juan Pablo Isaza 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!