Skip to content

Commit

Permalink
$lookup "from" must be null when using $documents pipeline (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
katcharov authored Oct 6, 2023
1 parent 33aa2ba commit a45742a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
4 changes: 2 additions & 2 deletions driver-core/src/main/com/mongodb/client/model/Aggregates.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public static Bson lookup(final String from, final String localField, final Stri
* the {@code from} collection is ignored.
*
* @param from the name of the collection in the same database to
* perform the join with. May be {$code null} if the
* perform the join with. Must be {$code null} if the
* first pipeline stage is $documents.
* @param pipeline the pipeline to run on the joined collection.
* @param as the name of the new array field to add to the input documents.
Expand All @@ -332,7 +332,7 @@ public static Bson lookup(@Nullable final String from, final List<? extends Bson
*
* @param <TExpression> the Variable value expression type
* @param from the name of the collection in the same database to
* perform the join with. May be {$code null} if the
* perform the join with. Must be {$code null} if the
* first pipeline stage is $documents.
* @param let the variables to use in the pipeline field stages.
* @param pipeline the pipeline to run on the joined collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,12 @@ public void testDocumentsLookup() {
getCollectionHelper().insertDocuments("[{_id: 1, a: 8}, {_id: 2, a: 9}]");
Bson documentsStage = Aggregates.documents(asList(Document.parse("{a: 5}")));

Bson lookupStage = Aggregates.lookup("ignored", Arrays.asList(documentsStage), "added");
Bson lookupStage = Aggregates.lookup(null, Arrays.asList(documentsStage), "added");
assertPipeline(
"{'$lookup': {'from': 'ignored', 'pipeline': [{'$documents': [{'a': 5}]}], 'as': 'added'}}",
"{'$lookup': {'pipeline': [{'$documents': [{'a': 5}]}], 'as': 'added'}}",
lookupStage);
assertEquals(
parseToList("[{_id:1, a:8, added: [{a: 5}]}, {_id:2, a:9, added: [{a: 5}]}]"),
getCollectionHelper().aggregate(Arrays.asList(lookupStage)));

// null variant
Bson lookupStageNull = Aggregates.lookup(null, Arrays.asList(documentsStage), "added");
assertPipeline(
"{'$lookup': {'pipeline': [{'$documents': [{'a': 5}]}], 'as': 'added'}}",
lookupStageNull);
assertEquals(
parseToList("[{_id:1, a:8, added: [{a: 5}]}, {_id:2, a:9, added: [{a: 5}]}]"),
getCollectionHelper().aggregate(Arrays.asList(lookupStageNull)));
}
}

0 comments on commit a45742a

Please sign in to comment.