3

I am trying to use mongoose js program to do the inserts, however I want to use 1,2,3,4 etc as the ids NOT to use the BSON ObjectID automatically created for me.

var mongoose = require('mongoose');

var dbHost = 'mongodb://localhost:27017/mong_db';

//var newID = mongoose.model('bookSchema', { _id: Number, name: String });

 mongoose.connect(dbHost);

 //Create a schema for Book

  var bookSchema = mongoose.Schema({

  _ id: Number, 

  name: String,

  //Also creating index on field isbn

  isbn: {type: String, index: true},

  author: String,

   pages: Number

  });

>Updated 


var book1 = new Book({

 _id = 1
name:"Mongoose Demo 1",

isbn: "MNG123",

author: "Author1,  Author2",

pages: 123

});
Robbie
  • 18,750
  • 4
  • 41
  • 45
  • Does this answer your question? [Mongoose auto increment](https://stackoverflow.com/questions/28357965/mongoose-auto-increment) – Dan Dascalescu Apr 15 '20 at 06:43

1 Answers1

5

You can specify the type used in your schema when you call the model constructor.

var Cat = mongoose.model('Cat', { _id: Number, name: String });

Then pass in an _id field when you store the object.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test-mongo-id');

var Cat = mongoose.model('Cat', { _id: Number, name: String });

var kitty = new Cat({ _id: 1, name: 'Zildjian' });
kitty.save(function (err) {
  console.log(err || 'meow');
  Cat.findOne({_id: 1}, function (err, cat) {
    console.log(err || 'Cat:', cat)
  });
});

Output:

meow
Cat: { _id: 1, name: 'Zildjian', __v: 0 }

You're schema should have _id: Number in it like this:

var bookSchema = mongoose.Schema({
  _id: Number,    // <-- You're missing this
  name: String,    
  //Also creating index on field isbn    
  isbn: {type: String, index: true},    
  author: String,    
  pages: Number    
});
Robbie
  • 18,750
  • 4
  • 41
  • 45
  • can you check my updated question if I did it right? –  Mar 02 '17 at 20:11
  • what does the schema for `Book` look like? e.g. `mongoose.model('Book', {..?..});` – Robbie Mar 02 '17 at 20:13
  • its correct except that its the schema for `newID` instead of `Book` – Robbie Mar 02 '17 at 20:16
  • and the `bookSchema` in the second part of your update is missing `_id: Number` – Robbie Mar 02 '17 at 20:19
  • I got an error: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html events.js:141 throw er; // Unhandled 'error' event ^ ValidationError: Book validation failed –  Mar 02 '17 at 20:19
  • Book.update({_id: Number, isbn : {$eq: 'MNG125'}}, {$set: {name: "Mongoose Demo 3.1"}}, function(err, result){ LIKE THIS –  Mar 02 '17 at 20:22
  • with your latest update, you are still missing `_id: Number,` from the bookSchema - i've updated my answer – Robbie Mar 02 '17 at 20:24
  • when you call update, pass the number you want, not the type `Number` - `Book.update( { _id: 1, ...})` – Robbie Mar 02 '17 at 20:26
  • should do it like that in my book part: _id = 1, –  Mar 02 '17 at 20:31
  • 1
    its difficult to know what you're asking when you keep changing the question around - please show the full code with details of exactly what is not working and any error messages you're getting - although if you just try what i've put in my answer, it will work. I just tried it - it works :) – Robbie Mar 02 '17 at 20:36
  • THANK YOU SO MUCH –  Mar 02 '17 at 20:37
  • 1
    You're welcome - you might want to give this a read before your next question ;) - http://stackoverflow.com/help/how-to-ask – Robbie Mar 02 '17 at 20:40
  • To also answer the `I want to use 1,2,3,4 etc` part of the question, you'd need to [generate auto-incrementing IDs](https://stackoverflow.com/questions/32685250/auto-increment-ids-in-mongoose/32686685#32686685), which requires a round-trip to the database. There's a reason MongoDB generates those unique IDs. – Dan Dascalescu Apr 13 '20 at 06:59