Query for documents where array size is greater than 1

后端 未结 14 1920
北海茫月
北海茫月 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:41
    db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
    

    you can use $gt and $lt in query.

    0 讨论(0)
  • 2020-11-22 03:43

    You can use aggregate, too:

    db.accommodations.aggregate(
    [
         {$project: {_id:1, name:1, zipcode:1, 
                     size_of_name: {$size: "$name"}
                    }
         },
         {$match: {"size_of_name": {$gt: 1}}}
    ])
    

    // you add "size_of_name" to transit document and use it to filter the size of the name

    0 讨论(0)
提交回复
热议问题