Query for documents where array size is greater than 1

后端 未结 14 1953
北海茫月
北海茫月 2020-11-22 03:02

I have a MongoDB collection with documents in the following format:

{
  \"_id\" : ObjectId(\"4e8ae86d08101908e1000001\"),
  \"name\" : [\"Name\"],
  \"zipcod         


        
14条回答
  •  后悔当初
    2020-11-22 03:27

    MongoDB 3.6 include $expr https://docs.mongodb.com/manual/reference/operator/query/expr/

    You can use $expr in order to evaluate an expression inside a $match, or find.

    { $match: {
               $expr: {$gt: [{$size: "$yourArrayField"}, 0]}
             }
    }
    

    or find

    collection.find({$expr: {$gte: [{$size: "$yourArrayField"}, 0]}});
    

提交回复
热议问题