[SERVER-6146] aggregation framework should have an $in expression Created: 20/Jun/12  Updated: 03/May/17  Resolved: 21/Apr/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.3.5

Type: New Feature Priority: Major - P3
Reporter: Aaron Staple Assignee: Benjamin Murphy
Resolution: Done Votes: 18
Labels: expression
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CSHARP-1634 aggregation framework should have an ... Closed
Documented
is documented by DOCS-9338 3.4: agg $in operator Closed
is documented by DOCS-9552 Docs for SERVER-6146: aggregation fra... Closed
Duplicate
is duplicated by SERVER-7162 Add $in operator for $cond, etc., in ... Closed
Related
related to DRIVERS-297 Aggregation Framework Support for 3.4 Closed
related to SERVER-8582 Extend document expression language i... Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 13 (04/22/16)
Participants:

 Description   

Syntax

{$in: [<arbitrary expression>, <arbitrary array expression>]}

Examples

db.coll.insert([
  {_id: 0, x: "Bob"}
]);
> db.coll.aggregate([{
  $project: {
    hasBobOrRob: {$in: ["$x", ["Bob", "Rob"]]}
  }
}]);
{_id: 0, hasBobOrRob: true}
 
// Example 2
> db.coll.insert([
  {_id: 0, x: "aaa"},
  {_id: 1, x: /^a/}
]);
> db.coll.aggregate([{
  $project: {
    matches: {$in: ["$x", [/^a/]]}
  }
}])
{_id: 0, matches: false}
{_id: 1, matches: true}
// Note the intentional inconsistency.
> db.foo.find({x: {$in: [/^a/]}})
{_id: 0, x: "aaa"}
{_id: 1, x: /^a/}

Notes

  • Should result in the same values as a $in in a MatchExpression (except with regexes).
  • Should not do regex matching if the values in the array are regexes.
  • This is different from what the query system does, a query predicate of {x: {$in: [/^a/]} will find strings which start with "a". This is arguably a bug in query, and will not be duplicated here.

Errors

  • If the second expression is not an array.
  • If there are not two arguments passed to the expression.

Old Description
Currently expressions like $eq, $ne, $gt, etc are supported but there is no $in available to test set membership.



 Comments   
Comment by Benjamin Murphy [ 21/Apr/16 ]

This patch introduces the $in expression for aggregation, which should be documented as part of the Aggregation Expressions page. In addition, any drivers that provide aggregation helpers should be updated to support $in.

Comment by Githook User [ 21/Apr/16 ]

Author:

{u'username': u'benjaminmurphy', u'name': u'Benjamin Murphy', u'email': u'benjamin_murphy@me.com'}

Message: SERVER-6146 Aggregation now supports the in expression.
Branch: master
https://github.com/mongodb/mongo/commit/223c2a1aa5fd11fd3e7115ca9acb6d1217f80f92

Comment by Asya Kamsky [ 01/Apr/16 ]

Finally ran into this myself - turns out the closest we have is using $setDifference - if "$a" is not $in "$array" then "$setDifference" of "$a","$array" will be ["$a"] if it is in "$array" then setDifference will be [ ]

Generated at Thu Feb 08 03:10:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.