user1532669
user1532669

Reputation: 2378

Meteor mongo query issue: MongoError: Invalid modifier specified $gt

I'm trying to write a mongo query to update a collection.

In SQL it would look something like this:

UPDATE myCollection SET count=(count-1)
WHERE otherCollectionId=otherCollection._id 
  AND otherCollectionUserId='HFDDEE78DFDSER34' 
  AND count > 0

I've tried a few things but this one is definitely not working. I'm getting the error:

MongoError: Invalid modifier specified $gt

My mongo query so far:

myCollection.update({otherCollectionId: otherCollection._id, 
                     otherCollectionUserId: Meteor.userId()
                    },
                    {$gt: {count: 0},
                     $inc: {count: -1}
                    });

Any ideas?

Upvotes: 0

Views: 138

Answers (1)

David Weldon
David Weldon

Reputation: 64312

I think this is probably what you want:

myCollection.update(
  {
    otherCollectionId: otherCollection._id,
    otherCollectionUserId: Meteor.userId(),
    count: {$gt: 0}
  },
  {$inc: {count: -1}},
  {multi: true}
);

All three parts of the selector are ANDed together, then we increment count by -1, and finally the query will run over multiple documents (not just the first one that matches the selector).

Upvotes: 1

Related Questions