Mongoid:如何返回符合特定条件的文件数量?

And*_*rew 4 mongodb mongoid

我需要返回一个计数列表(所有记录,所有记录都符合某个标准).我知道如何使用MySQL(SELECT COUNT(*) WHERE ...),但我不熟悉如何使用Mongo和Mongoid.如何以最有效的方式获取这些计数?

Pav*_*ler 7

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)