-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug - Mongoose >=4.7.3] Fail to create new record with string _id #4867
Comments
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 #4860 ...Although I have tried installing all dependencies so we get the latest version of bson and the bug persist... :( |
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:
|
@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:
|
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" ***@***.***> 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
<#4867 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADEBQ1mxBw2NUyaQg2D4dmJFp4mYEDSLks5rO7sBgaJpZM4LaO_u>
.
|
Seconding @thedewpoint above. The default for an unspecified +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 Tried with mongoose 4.7.3, 4.7.4, 4.7.6 This is the script I used to attempt to reproduce:
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" ***@***.***> wrote:
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: '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
<#4867 (comment)>,
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 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
...the |
@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: mongodb/js-bson#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) |
I have done this but get a mongoDb connection error mongoose.connect(
|
@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 |
When I try to create a new record with the "create" Model method by supplying the _id as a String,
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
The text was updated successfully, but these errors were encountered: