mongodb 如何在Mongo中查询“min(arr)>10”

frebpwbc  于 2023-08-04  发布在  Go
关注(0)|答案(2)|浏览(91)
db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

字符串
返回min(dim_cm)>15的文档
db.inventory.find( { dim_cm: { $all: {$gt:15} } } )似乎是错误的

w6lpcovy

w6lpcovy1#

相反,您应该使用$not$lte运算符来查找dim_cm包含所有大于15的元素的文档。

db.collection.find({
  dim_cm: {
    $not: {
      $lte: 15
    }
  }
})

字符串
Demo @ Mongo Playground
引用:单一查询条件

nnt7mjpx

nnt7mjpx2#

直观的语法是将$min$gt链接到$expr

db.inventory.find({
  $expr: {
    $gt: [
      {
        $min: "$dim_cm"
      },
      15
    ]
  }
})

字符串
Mongo Playground

相关问题