CastError: Cast to ObjectId failed for value "XXX" at path "_id" for model "XXX"
See original GitHub issueDo you want to request a feature or report a bug? Requesting a feature
What is the current behavior? _id is only accepting ObjectId even if we override it with string
What is the expected behavior? It need _id to accept strings rather than ObjectId
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. String in _id is supporting in Mongoose version - 5.12.6, which we are using. But it is not supported in Mongoose 5.12.7
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
Mongoose: CastError: Cast to ObjectId failed for value "[object ...
Short answer: use mongoose.Types.ObjectId. Mongoose (but not mongo) can accept object Ids as strings and "cast" them properly for you, so just use:...
Read more >cast to objectid failed for value "" (type string) at path "_id" for ...
Mongoose's findById method casts the id parameter to the type of the model's _id field so that it can properly query for the...
Read more >What's Mongoose error Cast to ObjectId failed for value XXX at ...
Node.js – Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”. Short answer: use mongoose.Types.ObjectId.
Read more >what's mongoose error cast to objectid failed for value yyy at ...
Controller.prototype.show = function(id, res) { this.model.findById(id, function(err, ... CastError: Cast to ObjectId failed for value "foo" at path "_id".
Read more >FreeCodeCamp/HelpBackEnd - Gitter
findOne({ 'github.id': profile.id }, function (err, user) { if (err) { return ... /whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The problem is that its not a valid _id string. It has to be either 12 byte binary string, or a 24 hex byte string.
ejeLoP_FH1620208025781
is neither. Using this function should help https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-isValidObjectIdIssue 1: Sample data: { “_id” : “ejeLoP_FH1620208025781”, firstName: “xxxx”, lastName : “yyyy” }
after installing the latest version, any updates to this collection throws an error “CastError: Cast to ObjectId failed for value “XXX” at path “_id” for model “XXX””
Issue 2: Sample data: const mongoose = require(“mongoose”); const schema = new mongoose.Schema({ _id:String, firstName: String, lastName: String });
Inserting a new Record, the result is { _id : ObjectId(“609269995b2e888426d019ef”), firstName: “xxxx”, lastName : “xxxx” }
But the expected result is, { _id : “609269995b2e888426d019ef”, firstName: “xxxx”, lastName : “xxxx” }
we have our own logic to generate the _id as a String, which is not happening. Instead, it is creating its own ObjectId. This is only happening after the latest release.