English 中文(简体)
Mongoid
原标题:Custom queries in Mongoid

是否有可能通过习惯性 j子,如发现的_子,或更新。

具体案例一:我想到inc。 a 符合标准的一组文件。

It looks like this is possible in MongoDB: http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations

db.people.update( { name:"Joe" }, { $inc: { created_at : -1 } } );

But I don t thing Mongoid allows it (it seems to only let you inc one document at a time). And their update_all method I believe only can use $set. Thanks! http://mongoid.org/docs/querying/modification.html

举例来说,这并不奏效。

User.where(:name => "Joe").update_all("{ $inc: { created_at : -1 } }")
最佳回答

Mongoid提供Model#inc方法,但采用了一种检验方法,而不是一种分类方法。

但是,通过阅读<代码>的源代码。 模型更新日期: 这种方法使你能够轻易地创建自己的<条码>inc。 班级方法。

def update_all(attributes = {})
  klass.collection.update(
    selector,
    { "$set" => attributes },
    Safety.merge_safety_options(:multi => true)
  ).tap do
    Threaded.clear_safety_options!
  end
end

As you can see, Mongoid is passing the query to the underlying mongodb driver. The following method should work

class User
  def self.inc(field, value)
    klass.collection.update(
      selector,
      { "$inc" => { field => value } },
      Safety.merge_safety_options(:multi => true)
    ).tap do
      Threaded.clear_safety_options!
    end
  end
end

请指出: 我已经对它进行了检测。

问题回答

B. 更新指挥时多国旗的投机者

http://www.mongodb.org

db.collection.update( criteria, objNew, upsert, multi )

multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

在你更新后指挥过程中树立了多条旗帜。

db.people.update( { name:"Joe" }, { $inc: { created_at : -1 } },false,true );




相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签