美文网首页MongoDB极简教程 · Mongo · NoSQL
MongoDB 数组(Array)长度,大小,为空,等相关处理

MongoDB 数组(Array)长度,大小,为空,等相关处理

作者: 沧海2122537190 | 来源:发表于2020-01-09 14:48 被阅读0次

示例数据

db.getCollection("Array").find()
//1
{"_id":"9bc57f0c-f1b2-11e6-a13b-005056882f99","vendor":["xjix"]}
//2
{"_id":"deab95fa-651a-11e6-b278-005056882f99","vendor":["vpdb","wfdbjournal"]}
//3
{"_id":"dea5fc4d-651a-11e6-b278-005056882f99","vendor":["vpdb","wfdbjournal"]}
//4
{"_id":"3a9c5877-7c58-4688-9c9e-6b6bfacc212d","vendor":["xjin","xjgzjournal","vpdb","wfdbjournal","xjsd"]}
//5
{"_id":"66455f2f-16f9-437d-b95b-2bfe1bf39b5b","vendor":["xjtf","wfdbjournal","xjsd"]}
//6
{"_id":"dea81e6f-651a-11e6-b278-005056882f99","vendor":["xjgzjournal","vpdb","wfdbjournal","xjsd"]}
//7
{"_id":"cc8b6309-a0c0-4ac7-8ebd-b862f25868f8","vendor":[]}

一、获取数组长度

$size方法

//语句
db.getCollection("Array").aggregate({$project:{_id:"$_id",vendor:"$vendor",cnt:{$size:"$vendor"}}})
//结果
//1
{"_id":"9bc57f0c-f1b2-11e6-a13b-005056882f99","vendor":["xjix"],"cnt":NumberInt("1")}
//2
{"_id":"deab95fa-651a-11e6-b278-005056882f99","vendor":["vpdb","wfdbjournal"],"cnt":NumberInt("2")}
//3
{"_id":"dea5fc4d-651a-11e6-b278-005056882f99","vendor":["vpdb","wfdbjournal"],"cnt":NumberInt("2")}
//4
{"_id":"3a9c5877-7c58-4688-9c9e-6b6bfacc212d","vendor":["xjin","xjgzjournal","vpdb","wfdbjournal","xjsd"],"cnt":NumberInt("5")}
//5
{"_id":"66455f2f-16f9-437d-b95b-2bfe1bf39b5b","vendor":["xjtf","wfdbjournal","xjsd"],"cnt":NumberInt("3")}
//6
{"_id":"dea81e6f-651a-11e6-b278-005056882f99","vendor":["xjgzjournal","vpdb","wfdbjournal","xjsd"],"cnt":NumberInt("4")}
//7
{"_id":"cc8b6309-a0c0-4ac7-8ebd-b862f25868f8","vendor":[],"cnt":NumberInt("0")}
结果图片

二、按长度查找

1、指定长度字段为具体数值

//示例一语句
db.getCollection("Array").find({vendor:{$size:2}})
//结果
//1
{"_id": "deab95fa-651a-11e6-b278-005056882f99","vendor": ["vpdb", "wfdbjournal"]}
//2
{ "_id": "dea5fc4d-651a-11e6-b278-005056882f99","vendor": ["vpdb","wfdbjournal" ]}

2、指定长度字段为范围区间

(1).$where方法

//vendor 的长度大于等于3,不可直接用this.vendor.length = 3
db.getCollection("Array").find({ $where: "this.vendor.length >= 3" })

(2).$exists方法

//判断vendor中第三个元素是否存在,数值索引从0开始
db.getCollection("Array").find({"vendor.2":{$exists:1} })//$exists:0 为不存在
//结果
//1
{"_id":"3a9c5877-7c58-4688-9c9e-6b6bfacc212d","vendor":["xjin","xjgzjournal","vpdb","wfdbjournal","xjsd"]}
//2
{"_id":"66455f2f-16f9-437d-b95b-2bfe1bf39b5b","vendor":["xjtf","wfdbjournal","xjsd"]}
//3
{"_id":"dea81e6f-651a-11e6-b278-005056882f99","vendor":["xjgzjournal","vpdb","wfdbjournal","xjsd"]}

(3).添加辅助列方法

//添加cnt记录数值长度,查找cnt大于等于3
db.getCollection("Array").aggregate([{$project:{_id:"$_id",vendor:"$vendor",cnt:{$size:"$vendor"}}},{$match:{cnt:{$gte:3}}}])
//结果
//1
{"_id":"3a9c5877-7c58-4688-9c9e-6b6bfacc212d","vendor":["xjin","xjgzjournal","vpdb","wfdbjournal","xjsd"],"cnt":NumberInt("5")}
//2
{"_id":"66455f2f-16f9-437d-b95b-2bfe1bf39b5b","vendor":["xjtf","wfdbjournal","xjsd"],"cnt":NumberInt("3")}
//3
{"_id":"dea81e6f-651a-11e6-b278-005056882f99","vendor":["xjgzjournal","vpdb","wfdbjournal","xjsd"],"cnt":NumberInt("4")}

三、按长度排序

添加辅助列排序

//添加cnt记录数值长度,对cnt进行排序
db.getCollection("Array").aggregate([{$project:{_id:"$_id",vendor:"$vendor",cnt:{$size:"$vendor"}}},{$sort:{cnt:-1}}])
添加辅助列方法

四、判断是否为空

1、数组为空

db.getCollection("Array").find({ $where: "this.vendor.length <= 0" })//数组length<= 0
db.getCollection("Array").find({"vendor.0":{$exists:0} })//数组第一个元素不存在
db.getCollection("Array").find({ "vendor": [] })//数组=[]
db.getCollection("Array").find({vendor:{$size:0}})//数组的size为零
db.getCollection("Array").find({ "vendor":{$not:{$elemMatch:{$ne:null}}}})//数组elemMatch是null
运行结果

2、数组非空

db.getCollection("Array").find({ $where: "this.vendor.length > 0" })//数组length> 0
db.getCollection("Array").find({"vendor.0":{$exists:1} })//数组第一个元素存在
db.getCollection("Array").find({ "vendor": {$gt:[] }})//数组大于[]
db.getCollection("Array").find({vendor:{$not:{$size:0}}})数组的size不为零
db.getCollection("Array").find({ "vendor":{$elemMatch:{$ne:null}}})//数组elemMatch不是null
运行结果

相关文章

网友评论

    本文标题:MongoDB 数组(Array)长度,大小,为空,等相关处理

    本文链接:https://www.haomeiwen.com/subject/tfebectx.html