The official mongoose v3.8.7 manual reads about Schema_id attribute, found that this attribute was generated when new was a model, and there was no communication with MongoDB at this time!
I wonder how mongoose generates the value of this _id. Can it guarantee uniqueness?
In addition, officials mentioned that this feature can be turned off, but
var schema = new Schema({ name: String }, { _id: false });
var Page = mongoose.model('Page', schema);
var p = new Page({ name: 'mongodb.org' });
console.log(p); // { name: 'mongodb.org' }
// MongoDB will create the _id when inserted
p.save(function (err) {
if (err) return handleError(err);
Page.findById(p, function (err, doc) {
if (err) return handleError(err);
console.log(doc); // { name: 'mongodb.org', _id: '50341373e894ad16347efe12' }
})
})
The actual test found that save will not succeed at all and will prompt:
[Error: document must have an _id before saving]
How to close _id and save it?
Each document must have an _id and cannot be repeated.
You turn off this feature and you have to create an _ID yourself.
So, of course, you can’t save it.