views:

22

answers:

1

How do I do the following in one operation:

  • Find or create object by some key:value pairs
  • Increment properties on the object.

I am doing this now:

Model.find_or_create_by_this_and_that(this, that).increment("a" => 1, "b" => 1)

What's the correct way to do that?

A: 

From javascript you should be able to do something like

db.model.update({"_id" : "xyz"}, {$inc : {"a":1,"b":1} })

It looks like the MongoMapper equivalent is

Model.collection.update({"_id" => self._id}, {"$inc" => {"a" => 1,"b" => 1}})

MongoMapper also seems to support an increment feature, but I'm unfamiliar with the syntax. In either case that second command looks very similar to the javascript version (and the php version), so that's probably what you're looking for.

Gates VP