MongoMapper,同时递增和更新其他属性?
如何在一项操作中执行以下操作:
- 通过某些键:值对查找或创建
- 对象 递增对象的属性。
我现在正在这样做:
Model.find_or_create_by_this_and_that(this, that).increment("a" => 1, "b" => 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过 javascript,您应该能够执行类似
db.model.update({"_id" : "xyz"}, {$inc : {"a":1,"b":1} })
看起来 MongoMapper 等效项是
Model.collection.update({"_id" => self._id}, {"$inc" => {"a" => 1,"b" => 1}})
MongoMapper 似乎也支持增量功能,但我不熟悉语法。无论哪种情况,第二个命令看起来都与 javascript 版本(和 php 版本)非常相似,所以这可能就是您正在寻找的。
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.