I’m using Casbah and Salat to create my own Mongodb dao and am implementing a getAll method like this:
val dao: SalatDAO[T, ObjectId]
def getAll(): List[T] = dao.find(ref = MongoDBObject()).toList
What I want to know is:
- Is there a better way to retrieve all objects?
- When I iterate through the objects, I can’t find the object’s _id. Is it excluded? How do I include it in the list?
1°/ The
ModelCompaniontrait provides adef findAll(): SalatMongoCursor[ObjectType] = dao.find(MongoDBObject.empty)methods. You will have to do a dedicated request for every collection your database have.If you iterate over the objects returned, it could be better to iterate with the
SalatMongoCursor[T]returned by thedao.findrather than doing two iterations (one with thetoListfromIteratortrait then another on yourList[T]).2°/ Salat maps the _id key with your class id field. If you define a class with an
id: ObjectIdfield. This field is mapped with the mongo _id key.You can change this behaviour using the
@Keyannotation as pointed out in Salat documentation