2016-12-01 180 views
1

文檔結構Mongoid查詢哈希領域 -與其他領域

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

我如何可以結合使用普通區查詢子句的哈希領域查詢條款?

感謝

+0

對不起,這是錯字,編輯 – user3775217

+0

是你看到的任何錯誤或者你沒有得到任何輸出它的文檔。您將這些查詢作爲AND條件進行組合的方式。所以如果你沒有得到輸出,請確保你有匹配的數據。但首先你想要查詢那些有通知類型feed true和用戶是當前用戶的人嗎? –

+0

你可以使用.any_of( {:notification_type.feed => true}, {:feed_user_type => current_user.id} )這將以或者你可以看到,如果你第一次得到輸出。 –

回答

1

這應該工作:

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

謝謝,這一個工作。 – user3775217