Bugs or developer error? Mongoose + Express

1,732 views
Skip to first unread message

Dan MacTough

unread,
Jul 6, 2011, 12:23:23 PM7/6/11
to mongoo...@googlegroups.com
I am using Mongoose 1.5.0 with Express 2.4.0 (and Node 0.4.9 and MongoDB 0.9.6-4). I am having fits trying to get Mongoose to do what I want it to do, and I am wondering if it's just me or if there are one or more bugs that I'm coming up against. Any help?

First:

My User schema definition contains { use$SetOnSave: false } so that I am able to use User.update() to, for example, increment a counter with { $inc: { counter: 1 } }. However, if I don't pass on of the options { safe: true } or { upsert: true }, then the record gets blown away -- only the _id is left, all the other fields are gone. It is NOT an upsert operation, so I don't know why the "upsert" option even works. I also don't understand what the "safe" option is doing. The source says that it enables the use of schema defaults -- which is a great option, should always be true by default, IMO --, but again, I am trying to increment a counter. Without one of those options, mongoose seems to think I should be sending the entire document (which I cannot even do in an update()).

Is this a bug or am I not grokking it?

Second:

Having defined my User schema with { use$SetOnSave: false }, I am not unable to use the method described here to update a record.

E.g.
User.findById( myId, function(err, doc) {
  // do some changes to doc
  doc.save(function(err){
    if (!err) {
      res.redirect('home'); // I don't think this is the wrong place. The process fails no matter where I put this.
    }
  });
});

Now, even though mongoose has the entire doc to save, it only seems to be saving a delta with only the properties that have been changed -- which means that all the other properties get blown away.

Here are some gists for your enjoyment:

1. Create the User record: https://gist.github.com/1067637  $ node test01.js
2a. Modify the record (no use$SetOnSave -- this works): https://gist.github.com/1067650 $ node test02.js <objectId>
2b. Modify the record ({ use$SetOnSave: false } -- this fails): https://gist.github.com/1067652 $ node test02b.js <objectId>

Any ideas? Bug?

Aaron Heckmann

unread,
Jul 8, 2011, 8:02:44 PM7/8/11
to mongoo...@googlegroups.com
First: a bug. use$SetOnSave: false has been pretty much neglected for a while.

There is an open ticket for getting Model.update() up to speed but for now you can just bypass mongoose and use the driver directly: User.collection.update(...)

Second: definitely also a bug.

I'd suggest keeping use$SetOnSave set to true and bypassing the driver as suggested above for direct updates until these issues get resolved.


--
Aaron


Dan MacTough

unread,
Jul 8, 2011, 11:54:46 PM7/8/11
to mongoo...@googlegroups.com
Okay. Thanks, Aaron. Glad it wasn't me being thick (this time).
Reply all
Reply to author
Forward
0 new messages