![](/img/trans.png)
[英]Find which field's size is greater than 20000 in a collection (mongodb)
[英]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 種類型的文檔
你可以做這樣的事情。 這是工作版本> 4.2
db.collection.find({
$expr: {
$gt: [
{
$size: "$type"
},
1
]
}
})
如果您使用較少,則可以執行以下操作
db.collection.find({
type: {
$gt: {
$size: 1
}
}
})
此問題的唯一有效解決方案如下:
db.collection.find({
$expr: {
$gt: [
{
$size: "$arrayfield"
},
1
]
}
})
所有其他解決方案都不起作用。 試過了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.