[DOCS-15565] Incorrect example for $filter agg expression? Created: 23/Aug/22  Updated: 30/Oct/23  Due: 09/Sep/22  Resolved: 21/Sep/22

Status: Closed
Project: Documentation
Component/s: manual, Server
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Bug Priority: Major - P3
Reporter: David Storch Assignee: Alison Huh
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 1 year, 21 weeks, 6 days ago
Epic Link: DOCSP-11701
Story Points: 1

 Description   

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 ]}
  }
}



 Comments   
Comment by Githook User [ 08/Sep/22 ]

Author:

{'name': 'Alison Huh', 'email': '112565127+ajhuh-mdb@users.noreply.github.com', 'username': 'ajhuh-mdb'}

Message: DOCS-15565-filter-agg-example (#1796)

Generated at Thu Feb 08 08:13:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.