Pedram
Pedram

Reputation: 81

Mongoid query on hash fields

I have a rails app with mongoid5. In my Project model has an attribute like this:

permissions : {
 "18748343" : 2,
 "23453744" : 3,
 "23453444" : 1
}

and an array like this

role_ids = ["123456", "23453744"]

I want to query on projects that has one of the role_ids members as key in their permissions attribute and value of that key is more than 1. How can I make this query with mongoid criteria?

Upvotes: 1

Views: 758

Answers (1)

ABrowne
ABrowne

Reputation: 1604

If you are using Mongoid::Attributes::Dynamic, mongoid5 will allow you to query like this:

Project.where('permissions.123456'.ne => nil)

This is because in mongo, permissions is document, with keys

Upvotes: 2

Related Questions