mongo.cr icon indicating copy to clipboard operation
mongo.cr copied to clipboard

How to find all documents from collection

Open dylandy opened this issue 7 years ago • 2 comments

In ruby mongo driver to find all documents in collection is

collection.find

However, in this driver expect to have some arguments pass to it. Which argument should I pass to find method in order to get the ideal result ?

dylandy avatar Feb 07 '18 02:02 dylandy

@dylandy here is how I did it: https://github.com/sam0x17/crystal-mongo-orm/blob/3262bc6880d7c6ec8e2f756bfc9a1a8050b71bc2/src/mongo_orm/querying.cr#L88

Note that my ORM is able to do this out of the box using the Model.all method, as you mention. https://github.com/sam0x17/crystal-mongo-orm

sam0x17 avatar Feb 07 '18 06:02 sam0x17

You can get all documents of a collection (as BSON objects) like this:

docs = [] of BSON
collection.find(BSON.new).each do |doc|
  docs << doc
end

I don't know if it's the best way, but it works fine.

Pyroglyph avatar Nov 28 '18 22:11 Pyroglyph