Ruby on rails Mongoid或/或任何意外行为
我对mongoidRuby on rails Mongoid或/或任何意外行为,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,我对mongoid的任何都有问题。我正在尝试查找一个字段大于0或另一个字段大于0的对象。我的问题是: Model.any_of(best_friend_method.gt => 0, method.gt => 0).desc(best_friend_method, method) 其“翻译”为: #[{:最好的朋友\u lc\u sum=>{“$gt”=>0},:lc\u sum=>{“$gt”=>0}]}, 选项:{:sort=>[[:最好的朋友\u lc\u sum,:desc
的任何
都有问题。我正在尝试查找一个字段大于0或另一个字段大于0的对象。我的问题是:
Model.any_of(best_friend_method.gt => 0, method.gt => 0).desc(best_friend_method, method)
其“翻译”为:
#[{:最好的朋友\u lc\u sum=>{“$gt”=>0},:lc\u sum=>{“$gt”=>0}]},
选项:{:sort=>[[:最好的朋友\u lc\u sum,:desc],:lc\u sum,:desc]},
类别:FB专辑,
嵌入:false>
据我所知,这就是我想要的。但它只返回6个结果Model.where(:best\u friends\u lc\u sum.gt=>0)。count
也会返回6个结果,但Model.where(:lc\u sum.gt=>0)。count
会返回850个对象
我希望我的查询返回这两者的结合:是mongoid/mongodb错误,还是我做错了什么
仅供参考:mongoid 2.4.5、mongodb 2.0.2、rails 3.1.3
谢谢你的时间 这是因为您只传递一个参数,而不是两个参数。这就好像你没有
$或用法
尝试:
在这种情况下,标准变为:
#<Mongoid::Criteria
selector: {"$or"=>[{:best_friends_lc_sum=>{"$gt"=>0}}, {:lc_sum=>{"$gt"=>0}}]},
options: {:sort=>[[:best_friends_lc_sum, :desc], [:lc_sum, :desc]]},
class: FbAlbum,
embedded: false>
#[{:best\u friends\u lc\u sum=>{“$gt”=>0}},{:lc\u sum=>{“$gt”=>0}]},
选项:{:sort=>[[:最好的朋友\u lc\u sum,:desc],:lc\u sum,:desc]},
类别:FB专辑,
嵌入:false>
有时,必须使用{}
来分隔不同的散列。如果这有助于某人……在Mongoid 3中,Origin gem提供了查询语法。这是你的电话号码。在这些方法中有或
方法,它允许您执行$或
查询:
# Mongoid query:
Model.or(
{ name: "Martin" }, { name: "Dave" }
)
# resulting MongoDB query:
{
"$or" => [
{ "name" => "Martin" }, { "name" => "Dave" }
]
}
使用OP的原始示例,可以将其改写为:
Model.or(
{ best_friend_method.gt => 0 },
{ method.gt => 0 }
).order_by(
best_friend_method,
method
)
传递给或方法的散列中至少有一个必须匹配,才能返回记录。是否查询正确的属性?似乎您在查询中使用了best_friend_方法和方法,但您的条件是best_friends_lc_sum和lc_sum。是的,需要它。我有相同的属性集,但使用lct
而不是lc
。我的方法应该查找给定的参数,并根据那些属性(lct属性或lc属性)选择将用于查询的属性。对不起,ksol,没有更多信息,mongoid构造的查询似乎是正确的,但我对数据模型和这些集合中的内容感到困惑。如果您直接对mongo运行查询,它会提供您想要的吗?
# Mongoid query:
Model.or(
{ name: "Martin" }, { name: "Dave" }
)
# resulting MongoDB query:
{
"$or" => [
{ "name" => "Martin" }, { "name" => "Dave" }
]
}
Model.or(
{ best_friend_method.gt => 0 },
{ method.gt => 0 }
).order_by(
best_friend_method,
method
)