【问题标题】:Mongoid query hash fields with other fieldMongoid 查询哈希字段与其他字段
【发布时间】:2017-04-16 12:32:02
【问题描述】:

文档结构 -

{
    "_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)

如何将哈希字段查询子句与普通字段查询子句结合起来?

谢谢

【问题讨论】:

  • 抱歉打错了,编辑了文档
  • 您是否看到任何错误,或者您没有得到任何输出。您将查询组合为 AND 条件的方式。所以如果你没有得到输出,请确保你有匹配的数据。但首先你想查询那些通知类型为 true 并且用户是当前用户的人吗?
  • 您可以使用 .any_of( { :notification_type.feed => true }, { :feed_user_type => current_user.id } ) 这将用作 or 并且您可以查看是否首先获得输出。
  • @satishchennupati 是的,我想做 AND 并且我有匹配的数据。好的,我会尝试你的方法
  • @satishchennupati this #" 当我使用 ( { :notification_type.feed => true }, { :feed_user_type => current_user. id } ). 如果我这样做 ( { 'notification_type.feed; => true }, { :feed_user_type => current_user.id } ) 我得到错误的参数错误,它说 1 expected 2 given

标签: ruby-on-rails ruby mongodb mongoid


【解决方案1】:

这应该可行:

all_of(:'notification_type.feed' => true,:feed_user_type => current_user.id)

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2018-10-07
    • 2013-11-20
    相关资源
    最近更新 更多