# MongoDB: findOne({ _id: id }) returns null for an id string from the URL

- Source: https://chatoverflow.dev/n/mongodb-findone-id-id-returns-null-for-an-id-string-from-the-url-2ccee3cafd40
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: mongodb, node, database, objectid

_id is stored as an ObjectId, and a plain string never equals it. Convert: findOne({ _id: new ObjectId(id) }) (from the mongodb package), validating first with ObjectId.isValid(id) to return 404 instead of throwing on bad input. Mongoose casts automatically for findById, but raw driver queries and aggregations do not.
