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
What is your _id defined as in your schema?
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"
I think this is related to the upgrade of the bson package. See here https://github.com/Automattic/mongoose/issues/4860
...Although I have tried installing all dependencies so we get the latest version of bson and the bug persist... :(
(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 [email protected]
@raugaral Thanks,it's work now!
Make sure that if using a string for the _id, that you also have the following defined in your schema:
_id: {type: String}
If you don't specify id in your schema it should by default be ObjectId,
also querying with a string instead of ObjectId should automatically cast,
but it isn't.
On Jan 4, 2017 10:30 AM, "joetidee" notifications@github.com wrote:
Make sure that if using a string for the _id, that you also have the
following defined in your schema:_id: {type: String}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Automattic/mongoose/issues/4867#issuecomment-270398584,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADEBQ1mxBw2NUyaQg2D4dmJFp4mYEDSLks5rO7sBgaJpZM4LaO_u
.
Seconding @thedewpoint above. The default for an unspecified _id
in your schema is an ObjectId. Before 4.7.3, strings were automatically cast into ObjectIds across all Mongoose functions (findByID
and others).
+1 for this issue.
ObjectId creation from a valid string fails. Can be reproduced in my case by:
require('mongoose').Types.ObjectId('AAAAAAAAAAAAAAAAAAAAAAAA');
It works if line 17 in node_modules/bson/lib/bson/objectid.js is changed to:
if (Buffer && Buffer.from('testString')) {
hasBufferType = true;
}
Everything goes back to this issue in the bson package, I'm afraid: https://github.com/mongodb/js-bson/issues/204
It seems if you can update to node 7.4.0 the issue is gone, as the Buffer.from function is working as expected by bson, which is used in mongoose.
Test (working in 7.4.0, probably not working in your node version):
console.log(
require('buffer').Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAA', 'hex')
);
I can't seem to reproduce this issue:
node v6.9.2
mongodb v3.2
Tried with mongoose 4.7.3, 4.7.4, 4.7.6
This is the script I used to attempt to reproduce:
// NPM Deps
const mongoose = require('mongoose');
const co = require('co');
const chalk = require('chalk');
// Constants
const GITHUB_ISSUE = `gh-4867`;
const STRING_AS_ID = '5788683c1fa46a472a051543';
// Set mongoose promise
mongoose.Promise = global.Promise;
try {
exec();
} catch (error) {
console.log(chalk.red(`Error: ${error}\n${error.stack}`));
}
function exec() {
return co(function* () {
mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`);
const ThingSchema = new mongoose.Schema({
name: String
});
const Thing = mongoose.model('Thing', ThingSchema);
const thing = yield Thing.create({
_id: STRING_AS_ID,
name: 'Bob'
});
console.log(chalk.green(`thing exists with id ${ STRING_AS_ID }: ${ !!thing }`));
const foundThing = yield Thing.findById(STRING_AS_ID);
console.log(chalk.green(`thing exists after finding by string id ${ STRING_AS_ID }: ${ !!foundThing }`));
}).catch(function(error) {
console.error(chalk.red(`Error ${error}\n${error.stack}`));
});
}
Does this script produce the same cast to ObjectId error?
I'm using node 4.4.x , maybe that's the difference
On Jan 5, 2017 9:37 AM, "Varun Jayaraman" notifications@github.com wrote:
I can't seem to reproduce this issue:
node v6.9.2
mongodb v3.2Tried with mongoose 4.7.3, 4.7.4, 4.7.6
This is the script I used to attempt to reproduce:
// NPM Deps
const mongoose = require('mongoose');
const co = require('co');
const chalk = require('chalk');// Constants
const GITHUB_ISSUE =gh-4867
;
const STRING_AS_ID = '5788683c1fa46a472a051543';// Set mongoose promise
mongoose.Promise = global.Promise;try {
exec();
} catch (error) {
console.log(chalk.red(Error: ${error}\n${error.stack}
));
}function exec() {
return co(function* () {
mongoose.connect(mongodb://localhost:27017/${GITHUB_ISSUE}
);const ThingSchema = new mongoose.Schema({ name: String }); const Thing = mongoose.model('Thing', ThingSchema); const thing = yield Thing.create({ _id: '5788683c1fa46a472a051543', name: 'Bob' }); console.log(chalk.green(`thing exists with id ${ thing._id.toString() }: ${ !!thing }`)); const foundThing = yield Thing.findById(thing._id.toString()); console.log(chalk.green(`thing exists after finding by string id ${ foundThing._id.toString() }: ${ !!foundThing }`));
}).catch(function(error) {
console.error(chalk.red(Error ${error}\n${error.stack}
));
});
}Does this script produce the same cast to ObjectId error?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Automattic/mongoose/issues/4867#issuecomment-270658060,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADEBQ2w05Gm5Arv7r70To0nazStoBm7zks5rPQAMgaJpZM4LaO_u
.
It definitely depends on the node version, as explained here https://github.com/mongodb/js-bson/issues/204 ... I'm not sure there's anything that can be done from this side (mongoose)... as AFAIK the issue comes from the bson package, which in turn is a dependency of mongodb-core (listed in the package.json with ^ which would explain why doing npm install
broke stuff for versions that worked before)
Im my case, it's node 4.4.4
This is the output of your script @varunjayaraman when run on my machine with mongoose 4.7.4 (bson 1.0.3) and node 4.4.4
Error ValidationError: CastError: Cast to ObjectID failed for value "5788683c1fa46a472a051543" at path "_id"
ValidationError: Thing validation failed
at MongooseError.ValidationError (/Users/javi/work/socialbro/node_modules/mongoose/lib/error/validation.js:23:11)
at model.Document.invalidate (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:1486:32)
at model.Document.set (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:753:10)
at model._handleIndex (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:596:14)
at model.Document.set (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:556:24)
at model.Document (/Users/javi/work/socialbro/node_modules/mongoose/lib/document.js:68:10)
at model.Model (/Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:47:12)
at new model (/Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:3238:13)
at /Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:1864:51
at /Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/internal/parallel.js:27:9
at eachOfArrayLike (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/eachOf.js:57:9)
at exports.default (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/eachOf.js:9:5)
at _parallel (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/internal/parallel.js:26:5)
at parallelLimit (/Users/javi/work/socialbro/node_modules/mongoose/node_modules/async/parallel.js:85:26)
at /Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:1882:5
at Function.create (/Users/javi/work/socialbro/node_modules/mongoose/lib/model.js:1852:17)
...the CastError
is indeed a hex is not a function
error coming from the bson package and being catched
@JvrBaena thanks, seems like it fails up to but not including node v6.0 on mongoose >= 4.7.3.
I also agree that this isn't a mongoose issue, but a problem with js-bson (through mongodb-core). This issue will hopefully be resolved soon: https://github.com/mongodb/js-bson/pull/205
For now the solution is to use mongoose < 4.7.2 or upgrade node >= 6.0. If anyone else has any other ideas, feel free to comment, but i think this and waiting for js-bson to merge in PR 205 are the best solutions.
Using node >=6.0 worked for me
I have this issue with node 6.9.3 and mongoose 4.7.6. But it works with mongoose 4.7.3.
Using node >=6.0 worked for me too (6.9.1)
(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 [email protected]
I have done this but get a mongoDb connection error check mongo-db connection Error: Invalid mongodb uri. Must begin with "mongodb://"
I can't use the dot env file like so DB_CONNECTION=mongodb+srv://<username>:<password>@cluster0.iqhpj.mongodb.net/test
This is the Express server code that connects perfectly on a higher version of mongoose ```const dBUri = process.env.DB_CONNECTION;
mongoose.connect(
dBUri,
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
},
(error) => {
if (!error) {
console.log("mongo-db connection working");
} else {
console.log("check mongo-db connection", error);
}
}
);
mongoose.Promise = global.Promise;
```
@SumiSastri you need to use Mongoose >= 5 to connect to MongoDB Atlas. Atlas only supports MongoDB >= 3.6, which requires Mongoose 5: https://mongoosejs.com/docs/compatibility.html
Most helpful comment
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"