Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose data saving without _id

I am using mongoose with node.js application. I don't want _id field in record. I am using this code to save my record without _id field. But it is giving error

document must have an _id before saving

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var PlayerSchema = new Schema({
    player_id :  { type: Number },
    player_name : { type: String },
    player_age  : { type: Number },
    player_country : { type: String }
}
, { _id: false }
);

var Player =  mongoose.model('Player', PlayerSchema );

        var athlete = new Player();
        athlete.player_id = 1;
        athlete.player_name = "Vicks";
        athlete.player_age  = 20;
        athlete.player_country = "UK";

        athlete.save(function(err) {
            if (err){
                console.log("Error saving in PlayerSchema"+ err);
            }
        });

I am using mongoose version 3.8.14

like image 221
mabc224 Avatar asked Aug 17 '14 00:08

mabc224


People also ask

When I do a post the data is not saved in MongoDB only ID is saved?

It means that Mongoose is looking for a value with the key of "any", literally. If your request body does not contain such key (and it does not, as it only contains name and age), it will not be picked up when you save the new object. What you are experiencing is the expected behaviour of Mongoose.

What does save () do in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

What is _ID in Mongoose?

By default, Mongoose adds an _id property to your schemas. const schema = new Schema(); schema.path('_id'); // ObjectId { ... } When you create a new document with the automatically added _id property, Mongoose creates a new _id of type ObjectId to your document. const Model = mongoose.

Does Mongoose create collection automatically?

exports = mongoose. model('User',{ id: String, username: String, password: String, email: String, firstName: String, lastName: String }); It will automatically creates new "users" collection.


2 Answers

Unfortunately, You can not skip having a primary key for the document but you can override the primary key content, you can define your own primary key for each document.

Try the following schema for the same.

var PlayerSchema = new mongoose.Schema({
_id :  { type: Number },
player_name : { type: String },
player_age  : { type: Number },
player_country : { type: String },

} );

I have replaced your player_id with _id. Now you have control over the primary key of the document and the system won't generate the key for you.

There are some plugins which can also do the autoincremet for your primary key. https://github.com/chevex-archived/mongoose-auto-increment. You might try these as well.

Also, about the error you are getting : Any document is an object and should be wrapped inside the curly brackets you can not define two independent object in the same document. So you are getting this error.

like image 168
Minkesh Jain Avatar answered Oct 01 '22 17:10

Minkesh Jain


Copying an answer given on a similar Github issue comment:

The _id option exists to prevent one being created and assigned to every sub-document within your document.

So, it's not for this:

var Schema = mongoose.Schema,
ThingSchema = new Schema({    
   name: String,  
   categories: [{         
      label: {            
         type: String,            
         uppercase: true      
      },      
      value: String
   }]
}, {_id: false});
//   ^^^^^^^^^
// this won't work 

It's for this:

var Schema = mongoose.Schema,
ThingSchema = new Schema({
    name: String,
    categories: [{
       label: {
          type: String,
          uppercase: true
       },
       value: String,
       _id: false // <--- this works
    }]
}); 

Took me a while but, after running into the same problem, I found the documentation here.

like image 36
OfirD Avatar answered Oct 01 '22 19:10

OfirD