-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
defaults are ignored on upsert #624
Comments
I probably should have mentioned... It's very likely I'm doing or understanding something wrong. I suppose I could simply query first, and insert or update as necessary. It just seems like a schema supported upsert would be something people would use frequently. |
This is the correct behavior but I think you're right that it used to send default values when you didn't specify them. That previous behavior was bad because we were potentially overwriting existing data with default values. On Nov 24, 2011, at 11:37 PM, Jordan Hoffreply@reply.github.com wrote:
|
But if you are knowingly telling it to do an upsert, and the field that you are querying by doesn't exsist, shouldn't it insert a new record? In what case would you be overwriting existing data? The update operation shouldn't have anything to do with default values. |
the upsert operation actually takes place on the db itself, it isn't a mongoose thing. re overwriting data: |
re overwriting data: var schema = new Schema({ name: String, number: { type: Number, default: 77 }})
var Thing = db.model('Thing', schema);
var t = new Thing({ name: "mongodb", number: 1 });
t.save(function () {
// t has been saved with number set to 1.
Thing.update({ name: "mongodb" }, { name: "MongoDB" }, { upsert: true }, function () {
// Mongoose will default this update to: { $set: { name: "MongoDB" }}
// Now the overriding part; if Mongoose passed defaults then we'd inadvertently be
// setting number to 77 which isn't the intention.
})
}) |
Got it, that makes sense. So the only way for mongoose to have an upsert method that included defaults, it would have to query the db prior to the update, which probably wouldn't make sense from a performance aspect. Thanks for the clarification. |
yeah depending on how much perf you need it might be ok. someone else just mentioned today that they might be putting a patch together for that so we'll see. |
As of 2.3.13, default values and type options that are specified in the schema are ignored during an upsert operation when the record is being inserted. Updates to existing records work as expected.
The text was updated successfully, but these errors were encountered: