Query on Mongoid Hash Field

Query on Mongoid Hash Field

Person.where('things.tv' => 'Samsung').first

This is where Mongoid and MongoDB really shine. Mongoid's Criteria methods (Person.where, Person.any_of, Person.excludes, etc.) will give you much more flexibility than the ActiveRecord-style finders (passing a :conditions hash to Person.find, Person.first, etc.)

Mongoid's site has some great documentation on how to use Criteria:

http://mongoid.org/en/mongoid/docs/querying.html

Mongoid query on hash fields

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

Mongoid query hash fields with other field

This should work:

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

Search Mongoid hash field by key

Course.where(:'skills_available.52ab84929938c7f966d4f0cc'.ne => nil)
  • Only restriction is that it will skip: {'52ab84929938c7f966d4f0cc' => nil}

mongodb query $in on a Hash field

Thanks to the previous answer I found the solution.

My field was defined like this

/**
* @MongoDB\Hash
*/
protected $durations;

But Hash type is defined for associatives array, and in my case only the values was important to me

So I change it to

/**
* @MongoDB\Collection
*/
protected $durations;

And after that I was able to do this query

{
"durations" : { $in: ['n11','n12']}
}

And I got relevant results.

Thanks to all of you

setting mongoid hash field values

The thing with Hash field is, it can be dynamic as much as you want. Therefore to prevent polluting your DB schema with unintended fields caused by bugs in your code this functionality is disabled by default.

No you are not stuck using 2-step updates for your hashes at all!

[],[]= are the shortcuts for read_attribute() and write_attribute() and should be used if you don't include Mongoid::Attributes::Dynamic. If you try to use $set without enabling dynamic attributes you will get a no-method error because it does not see your dynamic attributes as defined attributes.

If you'll read the source of Mongoid::Attributes::Dynamic then you'd find that this is required to add the dynamic attributes functionality.

To update the values by including Mongoid::Attributes::Dynamic you need to follow these steps:

thing = Thing.first
thing.set("info.endDate" => Time.now)
thing.reload # This will update the current variable

Otherwise if you need you can easily skip this and do the value update by 2-step method

I hope this sheds some light over your query.

Source:

Rails mongoid dynamic fields - no method error

Dynamic attributes with Rails and Mongoid

Using IN while querying on mongoid hash field

The query methods such as in and ne are added to Symbol, not String. So you can say:

:'keys.location_id'.in => location_ids

where location_ids is an array. Also, the query methods are just short forms for building the longer native-style nested Hash queries. For example, if you say:

:field.op => value

for some operator op, then Mongoid actually sends this (JavaScript) into MongoDB:

field: { $op: value }

So if you don't want to write :'keys.location_id'.in then you can say:

'keys.location_id' => { :$in  => location_ids }
# or
'keys.location_id' => { '$in' => location_ids }

instead.

Also note that if location_ids is an array then [location_ids] is an array of arrays and that's not what you want in your query, you want just location_ids as above.

How can I pass a variable into a mongoid query on a hash field

Ultimately we came up with this:

query = { :"hours.#{day}.close_hour" => { '$lt' => 23 } }
Business.where(query)

Source: How to use a variable as a field name in mongodb-native findOne()?

Thanks!

Edit: I'd like to point out that user 'mu is too short' recognised that it can just as easily be written like this:

:"hours.#{day}.close_hour".lt => 23

which is much closer to our original intent.



Related Topics



Leave a reply



Submit