繁体   English   中英

如何在mongodb中查找数组大小大于1的文档

[英]How to find the document in mongodb where array size is greater than 1

我想找到所有存在且数组大小大于 1 的文档

我的 MongoDB 集合看起来像

{
  "_id" : ObjectId("5eaaeedd00101108e1123452"),
  "type" : ["admin","teacher","student"]
}
{
  "_id" : ObjectId("5eaaeedd00101108e1123453"),
  "type" : ["student"], 
}

我如何找到超过 1 种类型的文档

db.collection.find({type: {$gt: 1}})

只需更改集合的名称


gt意味着更好,你可以在这里看到更多关于它的信息

你可以做这样的事情。 这是工作版本> 4.2

db.collection.find({
      $expr: {
        $gt: [
          {
            $size: "$type"
          },
          1
        ]
      }
    })

工作Mongo 游乐场

如果您使用较少,则可以执行以下操作

db.collection.find({
  type: {
    $gt: {
      $size: 1
    }
  }
})
  1. 你可以使用$gt

db.collectionName.find({"type": {$gt: 1} });

  1. 您可以使用$where

db.collectionName.find( { $where: "type > 1" } );

此问题的唯一有效解决方案如下:

db.collection.find({
  $expr: {
    $gt: [
      {
        $size: "$arrayfield"
      },
      1
    ]
  }
})

所有其他解决方案都不起作用。 试过了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM