我需要返回一个计数列表(所有记录,所有记录都符合某个标准).我知道如何使用MySQL(SELECT COUNT(*) WHERE ...),但我不熟悉如何使用Mongo和Mongoid.如何以最有效的方式获取这些计数?
Model.count
返回数据库中的文档数.如果要指定条件使用where
# Get the count of documents.
Person.count
# Get the count of documents given the provided conditions.
Person.where(title: "Sir").count
Run Code Online (Sandbox Code Playgroud)
来自MongoDB文档:
db.mycollection.count( {active:true}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3101 次 |
| 最近记录: |