skip to Main Content
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 ] }
]);

return document that min(dim_cm)>15

it seems db.inventory.find( { dim_cm: { $all: {$gt:15} } } ) is wrong

2

Answers


  1. Instead, you should work with $not and $lte operators to find the document with dim_cm contains all elements greater than 15.

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

    Demo @ Mongo Playground

    Reference: Single Query Condition

    Login or Signup to reply.
  2. An intuitive syntax would be chaining up $min and $gt in an $expr

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

    Mongo Playground

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search