如何在 Mongoid 中 运行 MongoDB 本机查询

How to run a MongoDB native query in Mongoid

好的,我想查询 MongoID 以再次检查一个参数。假设我有 start_dateend_date 作为模型的属性。我想像这样查询 Mongoid:

Model.where(start_date > end_date)

那我该怎么做呢?如果我要使用 MongoDB 控制台,我会做类似(未测试)的事情:

db.collection.find({$where: "this.end_date > this.start_date"}).count()

有什么想法吗?

你可以试试

Model.collection.find('$where' => 'this.end_date > this.start_date') 

或在 Mongoid 3 中为

Model.where('this.end_date > this.start_date')