簡體   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