I tried to run the code below, which I picked up from Mongoose documentation, and the execution not occur like expect. If I set schema option _id to false, mongoose must let me save without _id and then Mongodb will generate the _id for me, but instead, mongoose is throwing the following error "document must have an _id before saving".
var assert = require("assert");
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/noIDTest');
// disabled _id
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) {
assert.ifError(err);
Page.findById(p, function (err, doc) {
assert.ifError(err); // instead is printing this: Error: document must have an _id before saving
console.log(doc); // should print this: { name: 'mongodb.org', _id: '50341373e894ad16347efe12' }
})
});
Hmm where did you get this example from in the docs? Mongoose explicitly doesn't support saving documents without a top-level _id
, because there's no other way for us to know what document we're updating...
I have the same issue, I implemented a schema similar to the one found in http://mongoosejs.com/docs/guide.html#_id
var listSchema = new schema({
_name: {type: String, unique: true, required: true, index: true},
owner: {type: String, required: true, index: true},
visibility: {type: String, default: 'private'},
color: {type: String, default: '#e95e01'},
activ: {type: Boolean, default: true},
todos: []
}, {_id: false});
More of an issue with the docs than anything, mongoose doesn't really support saving a doc that doesn't have an _id
defined, because then we don't know how to update the doc.
oh ok, thanks for the clarification :)
I know this is old but, I stumbled across it with the same issue. I think the part of the explanation that is lacking is this:
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.
I really cant understand the "_because then we don't know how to update the doc_" sentence. The program will give mongoose enough criteria to update a document so mongoose should update document base on my first input in update
query. Then what is the problem with update what I say and ignore _id
? Like the original update in mongodb does.
This issue is about save()
, not update()
. save()
doesn't take a query filter as a parameter.
Most helpful comment
More of an issue with the docs than anything, mongoose doesn't really support saving a doc that doesn't have an
_id
defined, because then we don't know how to update the doc.