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?