使用count()
方法查询表中的记录条数,例如,下面的命令查询表users
的记录数量:spa
db.users.find().count();
当使用limit()
方法限制返回的记录数时,默认状况下count()
方法仍然返回所有记录条数。 例如,下面的示例中返回的不是5,而是user
表中全部的记录数量:code
db.users.find().skip(10).limit(5).count();
若是但愿返回限制以后的记录数量,要使用count(true)
或者count(非0)
:ip
db.users.find().skip(10).limit(5).count(true);
假设C1 表的数据以下:it
> db.c1.find() { "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 } { "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
那么执行如下命令就能够查询c1 表的数据量方法
> db.c1.count() 2
能够看出表中共有2 条数据。im