Genadinik
Genadinik

Reputation: 18649

Ruby and Mongo - how to query for a collection

I have a database connection that I got like this:

db = Mongo::Connection.new.db("app-development")

but when I tried querying for a collection like this:

@users = User.all.limit(50)

I got this error:

NoMethodError (undefined method `users' for #<Mongo::DB:0x10ed5f3b8>):

My collection is called users. What was the right way to get that data?

Thanks!

Upvotes: 0

Views: 1301

Answers (2)

Adrian
Adrian

Reputation: 1

Solution is this:

db = Mongo::Connection.new.db("app-development")

becomes

db = Mongo::Connection.new.db("app-development").collection("your_collection")

Then you will be acting on the database in the way you are imagining.

Upvotes: 0

Jim
Jim

Reputation: 54

Are you sure that you've got the right case on users? is it 'Users' or 'users'?

you could try:

@users = users.find("").limit(1);

This is the resource I used to learn... http://api.mongodb.org/ruby/current/file.TUTORIAL.html

Jim

Upvotes: 2

Related Questions