Mongoid 查询哈希字段与其他字段
Mongoid query hash fields with other field
文档结构 -
{
"_id" : "b3bf7422-4993-4728-ae6f-98f35aa52a9c",
"feed_user_type" : "user",
"relation" : "reported_by",
"notification_title" : "Thank you for submitting the report. The moderators will now review the evidence.",
"notification_type" : {
"email" : false,
"feed" : false,
"notification" : true
},
"updated_at" : ISODate("2016-12-01T12:14:41.269Z"),
"created_at" : ISODate("2016-12-01T12:14:41.269Z")
}
以下查询工作正常
Userfeed.where(:notification_type.feed => true).offset(offset).limit(size).asc(:created_at)
Userfeed.where(:feed_user_type => "user").offset(offset).limit(size).asc(:created_at)
第一个查询是查询哈希中的字段,第二个是简单查询,查询一个普通字段并给出正确的结果。
但是我结合了这两个查询我根本没有得到任何结果。我尝试了以下两种语法来组合上述查询子句
userfeeds = Userfeed.where(:notification_type.feed => true,:feed_user_type => current_user.id).offset(offset).limit(size).asc(:created_at)
userfeeds = Userfeed.where(:notification_type.feed => true).where(:feed_user_type => current_user.id).offset(offset).limit(size).asc(:created_at)
如何将散列字段查询子句与普通字段查询子句结合起来?
谢谢
这应该有效:
all_of(:'notification_type.feed' => true,:feed_user_type => current_user.id)
文档结构 -
{
"_id" : "b3bf7422-4993-4728-ae6f-98f35aa52a9c",
"feed_user_type" : "user",
"relation" : "reported_by",
"notification_title" : "Thank you for submitting the report. The moderators will now review the evidence.",
"notification_type" : {
"email" : false,
"feed" : false,
"notification" : true
},
"updated_at" : ISODate("2016-12-01T12:14:41.269Z"),
"created_at" : ISODate("2016-12-01T12:14:41.269Z")
}
以下查询工作正常
Userfeed.where(:notification_type.feed => true).offset(offset).limit(size).asc(:created_at)
Userfeed.where(:feed_user_type => "user").offset(offset).limit(size).asc(:created_at)
第一个查询是查询哈希中的字段,第二个是简单查询,查询一个普通字段并给出正确的结果。
但是我结合了这两个查询我根本没有得到任何结果。我尝试了以下两种语法来组合上述查询子句
userfeeds = Userfeed.where(:notification_type.feed => true,:feed_user_type => current_user.id).offset(offset).limit(size).asc(:created_at)
userfeeds = Userfeed.where(:notification_type.feed => true).where(:feed_user_type => current_user.id).offset(offset).limit(size).asc(:created_at)
如何将散列字段查询子句与普通字段查询子句结合起来?
谢谢
这应该有效:
all_of(:'notification_type.feed' => true,:feed_user_type => current_user.id)