Queries on arrays and hashes
We can easily search in arrays and hashes as we did with fields. For example, if a Book
class has
awards
, we can search in the awards
field. Let's see some code regarding the same:
irb> b = Book.first => #<Book _id: 516e7ab045db7cd86a000001, t(title): "Aristortle", ... published_date: nil, is_best_seller: false, awards: [], currency: nil> irb> b.awards => [] irb> b.awards << 'first place' => ["first place"] irb> b.awards << 'second place' => ["first place", "second place"] irb> b.awards << 'third place' => ["first place", "second place", "third place"] irb> b.save => true
Now, we can search in the awards
fields in the usual way as follows:
irb> Book.in(awards: ['second place']).first => #<Book _id: 516e7ab045db7cd86a000001, t(title): "Aristortle", published_date: nil, is_best_seller: false, awards: ["first place", "second place", "third place"], currency: nil>
However, searching...