$set or $inc on Update not working

2,374 views
Skip to first unread message

pbruna

unread,
Jun 1, 2011, 11:21:09 AM6/1/11
to Mongoose Node.JS ORM
When i run:

myDashboardStats.update(
{"name": request.domain},
{$set :{"requests" :10}},
{upsert: true},
fn();
)

The $set method does not work, and the collections get saved as:
{ "_id" : ObjectId("4de65586ae111f5f938a8222"), "$set" :
{ "requests" : 10 }, "name" : "170.139" }

I can't figure out why the $set method gets saved as a collection

Any ideas?

Thanks

Andrew Stone

unread,
Jun 1, 2011, 11:57:41 AM6/1/11
to mongoo...@googlegroups.com
Mongoose already wraps calls to update with $set so you just need to use:

myDashboardStats.update(
    {"name": request.domain},
    {"requests": 10},
    {upsert: true},
    fn()
);

Note that $inc and the like will not work properly because of the automatic use of $set by mongoose. So for inc you can use {requests: requests+1} instead of inc, although then you need to know the previous value ahead of time. Personally I like the native findAndModify function.

-Andrew

Patricio Bruna

unread,
Jun 1, 2011, 4:23:26 PM6/1/11
to mongoo...@googlegroups.com, mongoo...@googlegroups.com
So for now the best would be to use the native driver, or there is a way to call findAndModify from mongoose?

Patricio Bruna V.

Andrew Stone

unread,
Jun 1, 2011, 5:15:46 PM6/1/11
to mongoo...@googlegroups.com
You can use findAndModify and other native functions from within mongoose, but it's a little tricky.

You can get the collection from your schema and then call findAndModify on that. But you must init the results as mongoose models if you plan to use them with mongoose. Something like the following.

var schema = new mongoose.Schema({ ... });
schema.collection.findAndModify(args, function(error, doc) {
     if (err) return cb(err);
     if (!doc) return cb(null);
     var model = new schema();
     model.init(doc, function(err) {
         if (err) return cb(err);
         return cb(null, model);
     });
})

Jesse

unread,
Jun 21, 2011, 12:28:38 PM6/21/11
to Mongoose Node.JS ORM
Is this documented anywhere other than the list here? I would like to
start to pull goodies like the below and put them into a wiki.
Documentation is a bitch but I am starting a major endeavor using
mongoose and would like to offer my findings as I run into bumps a
long the way.

On Jun 1, 5:15 pm, Andrew Stone <andrew.j.ston...@gmail.com> wrote:
> You can use findAndModify and other native functions from within mongoose,
> but it's a little tricky.
>
> You can get the collection from your schema and then call findAndModify on
> that. But you must init the results as mongoose models if you plan to use
> them with mongoose. Something like the following.
>
> var schema = new mongoose.Schema({ ... });
> schema.collection.findAndModify(args, function(error, doc) {
>      if (err) return cb(err);
>      if (!doc) return cb(null);
>      var model = new schema();
>      model.init(doc, function(err) {
>          if (err) return cb(err);
>          return cb(null, model);
>      });
>
>
>
>
>
>
>
> })
> On Wed, Jun 1, 2011 at 4:23 PM, Patricio Bruna <pbr...@gmail.com> wrote:
> > So for now the best would be to use the native driver, or there is a way to
> > call findAndModify from mongoose?
>
> > Patricio Bruna V.
> >http://www.itlinux.cl
> > (+56-9) 8899 6618
>
> > El 01-06-2011, a las 11:57, Andrew Stone <andrew.j.ston...@gmail.com>

Andrew Stone

unread,
Jun 21, 2011, 12:40:16 PM6/21/11
to mongoo...@googlegroups.com
Unfortunately there isn't any documentation on this that I know of. I basically had to figure it out by looking at the code. 

Jesse

unread,
Jun 21, 2011, 1:56:27 PM6/21/11
to mongoo...@googlegroups.com
I aim to help with this. I am creating a knowledge base of this and other interesting tidbits I find while using mongoose and will hopefully cleanse them and get them into a wiki for others to contribute to. Maybe Guillermo et al can give the thumbs up or down? The docs are good but brief. I think a project wiki would be a good thing however as usual with documentation it does become necessary to keep it up to date.

Dan MacTough

unread,
Jun 21, 2011, 4:05:46 PM6/21/11
to Mongoose Node.JS ORM
Jesse, I just discovered this issue, too -- and the challenging state
of the documentation. I am planning to start submitting documentation
"patches" to the project on github as I come across issues like this.
I think that might be easier for mongoose users to find, rather than
yet another needle to find in the haystack. My 2 cents.

Aaron Heckmann

unread,
Jul 8, 2011, 8:58:35 PM7/8/11
to mongoo...@googlegroups.com
We so very much welcome documentation. What we have now is sorely outdated. And yes, much of that is my fault :)
--
Aaron


Dan MacTough

unread,
Jul 9, 2011, 12:07:06 AM7/9/11
to mongoo...@googlegroups.com
I was stymied in my attempt to understand "update" due to the bugs described here:
https://groups.google.com/forum/#!topic/mongoose-orm/G8i9S7E8Erg

When those bugs get fixed, I'll revisit this.

I love what mongoose does right, so I'm looking for any way to help.
Reply all
Reply to author
Forward
0 new messages