MongoDB判断null值,是否存在,Type操作符,最大最小值

使用 $exists操作符,是否存在

使用 exists 可以判断某个key是否存在

db.company.find({"address":{"$exists":true}})

可以用 null 结合使用

db.company.find({"address":{"$in":[null], "$exists":true}})

但是在pymongo中,不方便使用null作为特殊词,所以有方法2

使用 $type操作符

Mongo 中 type值 与数据类型的对应如下:
type定义说明
因此可以使用此判断是否为null

db.company.find({"address" : {$type : 10}})

当然也可以用来将 某个value的类型最为过滤条件。

最后再啰嗦一句,空字符不是null!!!

使用aggregate求最大值,最小值,平均值,总和

这里totalCount为需要计算的字段,使用aggregate来进行操作,具体的操作符此处为$max

db.getCollection('node_company_base').aggregate([{$group:{_id:'max',max_value:{$max:"$totalCount"}}}]);

1, 把$max换成其他如:sum、avg、min。可实现求和、平均、最小值功能
2, _id: ‘max’ 这里_id只要对应一个常量即可

猜你喜欢

转载自blog.csdn.net/iamcodingmylife/article/details/85098285
今日推荐