question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug - Mongoose >=4.7.3] Fail to create new record with string _id

See original GitHub issue

When I try to create a new record with the “create” Model method by supplying the _id as a String,

Model.create({
      _id: "5788683c1fa46a472a051543"
     name: "abc"
});

it fails with the following error: {"errors":{"_id":{"message":"Cast to ObjectID failed for value \"5788683c1fa46a472a051543\" at path \"_id\"","name":"CastError","stringValue":"\"5788683c1fa46a472a051543\"","kind":"ObjectID","value":"5788683c1fa46a472a051543","path":"_id","reason":{"message":"Cast to ObjectId failed for value \"5788683c1fa46a472a051543\" at path \"_id\"","name":"CastError","stringValue":"\"5788683c1fa46a472a051543\"","kind":"ObjectId","value":"5788683c1fa46a472a051543","path":"_id"}}},"message":"Model validation failed","name":"ValidationError"}

I suppose for Mongoose >= 4.7.3, it does not automatically convert string _id value to ObjectId _id value when we try to insert new records

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
thedewpointcommented, Jan 4, 2017

Also having this issue when retrieving records with a string ID using findById.

(err): CastError: Cast to ObjectId failed for value “586c6806a6918640e144622a” at path “_id” for model “User”

8reactions
raugaralcommented, Jan 4, 2017

(err): CastError: Cast to ObjectId failed for value “586c6806a6918640e144622a” at path “_id” for model "User

I have the same problem, at the moment I have “solve” it using the version 4.7.2. All posterior version have this bug. To install the version 4.7.2 you can use this command:

npm install mongoose@4.7.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose refuses to cast valid string to ObjectId
Turns out it's a bug in that version of mongoose. Removing my node_modules and reinstalling with mongoose set to 4.3.3 fixed this issue....
Read more >
cast to objectid failed for value "undefined" (type ... - You.com
Describe the bug. I tried to add a new record/data in Strapi v3.6.6 with MongoDB Atlas.
Read more >
Using TypeScript with Mongoose
Create a Schema corresponding to the document interface. const userSchema = new Schema<IUser>({ name: { type: String ...
Read more >
07 - Use model.findById() to Search Database By _id - YouTube
MongoDB assigns each document a unique id on creation. We can call the findById() method on a model, with an id to pick...
Read more >
types/mongoose/index.d.ts - UNPKG
351, * @event error Emitted when an error occurs on this connection. 352, * @event fullsetup Emitted in a replica-set scenario, when primary...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found