MongoMapper returns empty document set on call to `all`

54 Views Asked by At

I have a collection in mongodb:

> use my_db
switched to db my_db
> db.my_coll.count()
278

I have a class in ruby:

class MyColl
  include MongoMapper::Document
  key :fst, Integer
  key :sec, Float
end

When I call:

MongoMapper.database = 'my_db'
MyColl.count
#=> 0

though I definitely expected to receive the same result as in mongo console. No errors, no nothing.

What am I missing here?

1

There are 1 best solutions below

0
Aleksei Matiushkin On BEST ANSWER

MongoMapper's ActiveRecord convention expects class name to correspond collection name as:

self.class.name.gsub(/::/, '.').underscore.pluralize == coll_name

That said, class MyColl expects the underlying collection to be named my_colls. Despite that there is no such collection in the database, MongoMapper silently assumes the user wants to create this collection transparently. That's why MyColl.count returns zero, rather than throws an exception.

> db.my_coll.renameCollection("my_colls")

will do the trick.