Mongoid finders not working? Mongoid finders not working? mongodb mongodb

Mongoid finders not working?


Okay, so this is part of what makes mongoid irritating for newcomers. People expect methods like User.all to actually return an array when it really just returns the Criteria object.

In order to provide the syntatic sugar of chainable methods and other fancy query mechanisms, Mongoid seems to use a lazy loading type thing.

You can do:

#array indexUser.all[0]#first/lastUser.all.first#each over things, print out all the usersUser.all.each {|u| p u}#edit, I forgot to include this, which is probably what you really want#this spits out an arrayUser.all.to_a

It makes it difficult to quickly verify that things are working for newcomers from ActiveRecord where User.all just returns an array.


Try this:

    User.all.first            User.find(:first, :conditions => {:first_name => 'John'})        User.where(:first_name => 'John').first