发布于 1年前

MongoDB判断数组是否为空的几种方法

记录下MongoDB用于判断数组是否为空的语句。

方法一:{"field1.0":{$exists: true}} (推荐)

db.testcollection.find({"field1.0":{$exists: true}})

方法二:$elemMatch:{$ne:null}

db.testcollection.find({"field1":{$elemMatch:{$ne:null}}})

方法三:$where:"this.field1.length>0"

db.testcollection.find({$where:"this.field1.length>0"})

方法四:{"field1":{$gt: []}}

db.testcollection.find({"field1":{$gt: []}})
©2020 edoou.com   京ICP备16001874号-3