Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set _id to db document in Mongoose?

I'm trying to dynamically create _id's for my Mongoose models by counting the documents in the db, and using that number to create the _id (assuming the first _id is 0). However, I can't get the _id to set from my values. Here's my code:

//Schemas var Post = new mongoose.Schema({     //_id: Number,     title: String,     content: String,     tags: [ String ] });  var count = 16;  //Models var PostModel = mongoose.model( 'Post', Post );  app.post( '/', function( request, response ) {      var post = new PostModel({         _id: count,         title: request.body.title,         content: request.body.content,         tags: request.body.tags     });      post.save( function( err ) {         if( !err ) {             return console.log( 'Post saved');         } else {             console.log( err );         }     });      count++;      return response.send(post); }); 

I've tried to set the _id a number of different ways, but it's not working for me. Here's the latest error:

{ message: 'Cast to ObjectId failed for value "16" at path "_id"',   name: 'CastError',   type: 'ObjectId',   value: 16,   path: '_id' } 

If you know what's going on, please let me know.

like image 525
EmptyArsenal Avatar asked Nov 04 '13 02:11

EmptyArsenal


People also ask

Can I set _ID in Mongoose?

You can also overwrite Mongoose's default _id with your own _id . Just be careful: Mongoose will refuse to save a document that doesn't have an _id , so you're responsible for setting _id if you define your own _id path.

What is _ID in Mongoose?

Sep 3, 2019. By default, MongoDB creates an _id property on every document that's of type ObjectId. Many other databases use a numeric id property by default, but in MongoDB and Mongoose, ids are objects by default.

Can I set default value in Mongoose schema?

You can also set the default schema option to a function. Mongoose will execute that function and use the return value as the default.

Can _ID be a string MongoDB?

According to the Document page in the MongoDB manual: The _id field may contain values of any BSON data type, other than an array, regex, or undefined. So yes, you should be able to use a string as a document _id field.


2 Answers

You either need to declare the _id property as part of your schema (you commented it out), or use the _id option and set it to false (you're using the id option, which creates a virtual getter to cast _id to a string but still created an _id ObjectID property, hence the casting error you get).

So either this:

var Post = new mongoose.Schema({     _id: Number,     title: String,     content: String,     tags: [ String ] }); 

Or this:

var Post = new mongoose.Schema({     title: String,     content: String,     tags: [ String ] }, { _id: false }); 
like image 133
robertklep Avatar answered Sep 24 '22 12:09

robertklep


The first piece of @robertklep's code doesn't work for me (mongoose 4), also need to disabled _id

var Post = new mongoose.Schema({   _id: Number,   title: String,   content: String,   tags: [ String ] }, { _id: false }); 

and this works for me

like image 28
timqian Avatar answered Sep 24 '22 12:09

timqian