1
votes

Document structure -

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

Following queries works fine

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)

First query is querying the field in the hash and second one is simple query querying a normal field and gives proper results.

But i combine both the queries i don't get any results at all. I tried following two syntax for combining the above query clause

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)

How can i combine a hash field query clause with normal field query clause?

Thanks

1
sorry that was typo, edited the doc - user3775217
are you seeing any error or is it you are not getting any output. The way you are combining the queries they work as AND condition. so if you are not getting output make sure you have matching data. but on the first place do you want to query for those who have notification-type feed true and user is the current user ? - satish chennupati
you can use .any_of( { :notification_type.feed => true }, { :feed_user_type => current_user.id } ) this will work as or and you can see if you first get output. - satish chennupati
@satishchennupati yes i want to do AND and i have matching data. Ok i will try your approach - user3775217
@satishchennupati this #<NoMethodError: undefined method `feed' for :notification_type:Symbol>" error comes when i use ( { :notification_type.feed => true }, { :feed_user_type => current_user.id } ). If i do ( { 'notification_type.feed; => true }, { :feed_user_type => current_user.id } ) i get wrong argument error which says 1 expected 2 given - user3775217

1 Answers

1
votes

This should work:

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