WagnerMatosUK
WagnerMatosUK

Reputation: 4429

How to search for documents which has a specific attribute inside hash Mongoid 4

Imagine the following Model:

class Model
  ...
  field :things,    type Hash
  ...
end 

I would like then to be able to search for documents which has a specific attribute inside the things hash like so:

model = Model.where('things.order_id' => 9689689)

or

model = Model.where('things.order_id' => 9689689).all

But that doesn't find the document(s) I'm looking for. Neither does the following:

model = Model.elem_match(inside: {order_id: 116152})

nor this one:

model = Model.where(:inside.elem_match => {order_id: 116152})

What am I missing here?

Please note this is not an embedded document, but simply a model which contains hashes (though would be nice to find a way to do similar searches in embedded models).

Any ideas?

Upvotes: 0

Views: 26

Answers (1)

Rubysmith
Rubysmith

Reputation: 1175

model = Model.where(:'things.order_id' => 9689689)

Upvotes: 1

Related Questions