mongodb查询非空数组

这个东西用的好少,每次要用的时候都想不起来,记一下吧

orderHistory是个数组:

  方法一:  db.photos.find({"orderHistory":{$elemMatch:{$ne:null}}},{orderHistory:1})

        重点: $elemMatch:{$ne:null}

  方法二:  db.photos.find({$where:"this.orderHistory.length>0"})

        重点:$where:"this.orderHistory.length>0"

  方法三:  db.photos.find({"orderHistory.0":{$exists: true}})

        重点:这个最简单

  方法四:  db.photos.find({"orderHistory":{$gt: []}})

        重点:。。。。。。

  方法五:  db.photos.find({"orderHistory":{$not: {$size: 0}}})

 

  恩,就这样

原文地址:https://www.cnblogs.com/JohannaFeng/p/6598385.html