How do I get the objectID after I save an object in Mongoose?

This just worked for me:

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

mongoose.connect('mongodb://localhost/lol', function(err) {
    if (err) { console.log(err) }

var ChatSchema = new Schema({
    name: String

mongoose.model('Chat', ChatSchema);

var Chat = mongoose.model('Chat');

var n = new Chat(); = "chat room";,room) {

$ node test.js

I’m on mongoose 1.7.2 and this works just fine, just ran it again to be sure.

Leave a Comment