I need to return a list of counts (all records, all records matching a certain criteria). I know how to do this using MySQL (SELECT COUNT(*) WHERE ...
) but I'm not familiar with how to do this using Mongo and Mongoid. How can I fetch these counts in the most efficient way?
From the Mongoid documentation:
Model.count
Returns the number of documents in the database. If you want to specify conditions use where
# Get the count of documents.
Person.count
# Get the count of documents given the provided conditions.
Person.where(title: "Sir").count
From MongoDB documentation:
db.mycollection.count( {active:true}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With