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

defaults are ignored on upsert #624

Closed
jhoff opened this issue Nov 25, 2011 · 7 comments
Closed

defaults are ignored on upsert #624

jhoff opened this issue Nov 25, 2011 · 7 comments

Comments

@jhoff
Copy link

jhoff commented Nov 25, 2011

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.

//schema declaration

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

var ip_address = "0.0.0.0";

exports.beta_email = new Schema({
    email : { type: String, index: true, uppercase: true },
    ip_address : { type: String, match: /[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}/, 'default': ip_address },
    first_submit : { type: Date, 'default': Date.now },
    total_submits : { type: Number, 'default': 1 },
}, { collection: 'beta_email' });

//upsert ( does not write fields with default values ( first_submit, ip_address ) and leaves the email in lowercase )

db.model('beta_email')
    .update(
        { email: req.body.email },
        { $inc: { total_submits: 1 } },
        { upsert: true, multi: false },
        function(err) {}
    );

//produces

{
   "_id": ObjectId("4ecf18651b77a21f69254acf"),
   "email": "upsert@event.com",
   "total_submits": 1
}


// save 

var Beta_Email = db.model('beta_email');
var be = new Beta_Email();
be.email = req.body.email;
be.save( function(err) {} );

//produces this:

{
   "email": "SAVE@EVENT.COM",
   "_id": ObjectId("4ecf1883f04792b1a5000002"),
   "ip_address": "0.0.0.0",
   "total_submits": 1,
   "first_submit": ISODate("2011-11-25T04: 24: 35.707Z")
} 
@jhoff
Copy link
Author

jhoff commented Nov 25, 2011

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.

@aheckmann
Copy link
Collaborator

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:

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.

//schema declaration

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

var ip_address = "0.0.0.0";

exports.beta_email = new Schema({
   email : { type: String, index: true, uppercase: true },
   ip_address : { type: String, match: /[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}/, 'default': ip_address },
   first_submit : { type: Date, 'default': Date.now },
   total_submits : { type: Number, 'default': 1 },
}, { collection: 'beta_email' });

//upsert ( does not write fields with default values ( first_submit, ip_address ) and leaves the email in lowercase )

db.model('beta_email')
   .update(
       { email: req.body.email },
       { $inc: { total_submits: 1 } },
       { upsert: true, multi: false },
       function(err) {}
   );

//produces

{
  "_id": ObjectId("4ecf18651b77a21f69254acf"),
  "email": "upsert@event.com",
  "total_submits": 1
}


// save 

var Beta_Email = db.model('beta_email');
var be = new Beta_Email();
be.email = req.body.email;
be.save( function(err) {} );

//produces this:

{
  "email": "SAVE@EVENT.COM",
  "_id": ObjectId("4ecf1883f04792b1a5000002"),
  "ip_address": "0.0.0.0",
  "total_submits": 1,
  "first_submit": ISODate("2011-11-25T04: 24: 35.707Z")
} 

Reply to this email directly or view it on GitHub:
#624

@jhoff
Copy link
Author

jhoff commented Dec 9, 2011

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.

@aheckmann
Copy link
Collaborator

the upsert operation actually takes place on the db itself, it isn't a mongoose thing.

re overwriting data:

@aheckmann
Copy link
Collaborator

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.
  }) 
})

@jhoff
Copy link
Author

jhoff commented Dec 13, 2011

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.

@jhoff jhoff closed this as completed Dec 13, 2011
@aheckmann
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants