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

If you explicitly declare _id: Schema.ObjectId for your model, then the ObjectId will not be available after new or save. #1285

Closed
totty90 opened this issue Jan 11, 2013 · 6 comments

Comments

@totty90
Copy link

totty90 commented Jan 11, 2013

If you explicitly declare

_id: Schema.ObjectId

for your model, then the ObjectId will not be available after new or save.

@aheckmann
Copy link
Collaborator

please post code to reproduce.

@totty90
Copy link
Author

totty90 commented Jan 14, 2013

Source: http://stackoverflow.com/questions/6074245/node-mongoose-get-last-inserted-id
But here is the code too:

var SerialSchema = new Schema({
     _id        : ObjectId
    ,serial     : String
    ,year       : Number
}, { strict: true
    ,toObject: {
      virtuals: true
    }
    ,toJSON: {
      virtuals: true
    }
});

var serial = new Serial({serial: 'abc', year: 2012});
serial.save(function(err, _serial){
    // no error
    // serial._id or serial.id is not defined
    // _serial._id or _serial.id is not defined too
})

@totty90
Copy link
Author

totty90 commented Jan 14, 2013

"version": "3.4.0",

@aheckmann
Copy link
Collaborator

ah, the docs were incorrectly hiding the option you need (will be fixed next time they're published).

you must pass the auto option like so:

new Schema({ _id: { type: Schema.ObjectId, auto: true }})

aheckmann added a commit that referenced this issue Feb 12, 2013
should have been already.

relates to #1285
@totty90
Copy link
Author

totty90 commented Feb 12, 2013

thanks

aheckmann added a commit that referenced this issue Mar 2, 2013
should have been already.

relates to #1285
@jeshuneo
Copy link

jeshuneo commented Dec 3, 2019

new Schema({ _id: { type: Schema.ObjectId, auto: true }})
"CHANGE (_id) to (id)"

@Automattic Automattic locked as resolved and limited conversation to collaborators Dec 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants