Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-15565

Incorrect example for $filter agg expression?

      Looks like there's a typo in the "Behavior" section of the reference page for the $filter agg expression: https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#behavior

      The second two examples are the following:

      // The docs claim this will return [ 1, 2, 3.1, NumberLong(4) ].
      {
        $filter: {
           input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
           as: "num",
           cond: { $and:[
              { $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
              { $gte: [ "$$num", NumberLong("9223372036854775807") ] }
           ] }
           limit: 2
        }
      }
      
      // The docs claim this will return [ 1 ].
      {
        $filter: {
           input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
           as: "num",
           cond: { $and:[
              { $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
              { $gte: [ "$$num", NumberLong("9223372036854775807") ] }
           ] }
           limit: { $add: [ 0, 1 ]}
        }
      }
      

      I think the second $gte should be a $lte for both example expressions? Also, both are missing a comma after the cond expression. So they should look like this:

      // The docs claim this will return [ 1, 2, 3.1, NumberLong(4) ].
      {
        $filter: {
           input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
           as: "num",
           cond: { $and:[
              { $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
              { $lte: [ "$$num", NumberLong("9223372036854775807") ] }
           ] },
           limit: 2
        }
      }
      
      {
        $filter: {
           input: [ 1, "a", 2, null, 3.1, NumberLong(4), "5" ],
           as: "num",
           cond: { $and:[
              { $gte: [ "$$num", NumberLong("-9223372036854775807") ] },
              { $lte: [ "$$num", NumberLong("9223372036854775807") ] }
           ] },
           limit: { $add: [ 0, 1 ]}
        }
      }
      

            Assignee:
            alison.huh@mongodb.com Alison Huh
            Reporter:
            david.storch@mongodb.com David Storch
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              1 year, 34 weeks, 2 days ago