温馨提示×

mongodb中count怎么用

小晨
1430
2021-07-12 17:16:07
栏目: 云计算

mongodb中count的使用方法:在mongodb中对于大量的数据,可以使用count()方法对数据进行统计,语法格式为:“db.集合名称.find({条件}).count()”和“db.集合名称.count({条件})”,例如:“db.yisu.find({name:{$ne:null}}).count()”统计yisu集合中name字段的数据记录。

mongodb中count怎么用

具体内容如下:

一、count()方法介绍

1、作用:用于统计结果集中文档条数

2、count()方法两种使用语法

db.集合名称.find({条件}).count()

db.集合名称.count({条件})

3、返回值

返回一个包含计数和命令状态的文档。

二、count()方法使用实例

> db.foo.find({name:{$ne:null}})

{ "_id" : ObjectId("544db3b45d92133398a80dab"), "a" : 1, "name" : "zzz" }

> db.foo.find({name:{$ne:null}}).count()             #查出个数为1,正确的

1

> db.foo.find({name:{$ne:null}}).skip(2)             #查不出数据

> db.foo.find({name:{$ne:null}}).skip(2).count()      #查出个数为1,不正确

1

> db.foo.find({name:{$ne:null}}).skip(2).count(1)    #查出个数为0,正确

0

出现统计慢的现象,则加上projection即可处理。

db.trip_product.find({"supplierId":{$in:["ziying", "ycf", "dfy"]}, "remove":0},

{"remove":1,"_id":0}).itcount()

0