Skip to content
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

Closed
kyofight opened this issue Jan 4, 2017 · 21 comments
Closed

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

kyofight opened this issue Jan 4, 2017 · 21 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@kyofight
Copy link

kyofight commented Jan 4, 2017

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

@joetidee
Copy link

joetidee commented Jan 4, 2017

What is your _id defined as in your schema?

@thedewpoint
Copy link

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"

@JvrBaena
Copy link

JvrBaena commented Jan 4, 2017

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... :(

@raugaral
Copy link

raugaral commented 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

@ifyour
Copy link

ifyour commented Jan 4, 2017

@raugaral Thanks,it's work now!

@joetidee
Copy link

joetidee commented Jan 4, 2017

Make sure that if using a string for the _id, that you also have the following defined in your schema:

_id: {type: String}

@thedewpoint
Copy link

thedewpoint commented Jan 4, 2017 via email

@JCBird1012
Copy link

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.

@foriam
Copy link

foriam commented Jan 5, 2017

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;
}

@JvrBaena
Copy link

JvrBaena commented Jan 5, 2017

Everything goes back to this issue in the bson package, I'm afraid: https://github.com/mongodb/js-bson/issues/204

@foriam
Copy link

foriam commented Jan 5, 2017

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')
);

@sobafuchs
Copy link
Contributor

sobafuchs commented Jan 5, 2017

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?

@sobafuchs sobafuchs added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jan 5, 2017
@thedewpoint
Copy link

thedewpoint commented Jan 5, 2017 via email

@JvrBaena
Copy link

JvrBaena commented Jan 5, 2017

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

jeffcooper86 added a commit to jeffcooper86/heyfeedme.com that referenced this issue Jan 5, 2017
@sobafuchs sobafuchs removed the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jan 6, 2017
@sobafuchs
Copy link
Contributor

@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

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 6, 2017
@sobafuchs sobafuchs added this to the 4.8 milestone Jan 6, 2017
@sobafuchs
Copy link
Contributor

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.

@vkarpov15 vkarpov15 modified the milestones: 4.7.7, 4.8 Jan 6, 2017
@sobafuchs sobafuchs modified the milestones: 4.7.7, 4.10 Jan 6, 2017
@kapilgarg1996
Copy link

Using node >=6.0 worked for me

@Airboy
Copy link

Airboy commented Jan 6, 2017

I have this issue with node 6.9.3 and mongoose 4.7.6. But it works with mongoose 4.7.3.

@ghost
Copy link

ghost commented Jan 10, 2017

Using node >=6.0 worked for me too (6.9.1)

@SumiSastri
Copy link

SumiSastri commented Sep 3, 2020

(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

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;

@vkarpov15
Copy link
Collaborator

@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

@Automattic Automattic locked and limited conversation to collaborators Sep 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests