Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Stream<Document> apply(Stream<Document> stream) {
}

private Document resolveRemoteField(Document document) {
Object value = Utils.getSubdocumentValue(document, localField);
Object value = Utils.getSubdocumentValueCollectionAware(document, localField);
List<Document> documents = lookupValue(value);
Document result = document.clone();
result.put(as, documents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ public void testLookupWithNestedField() throws Exception {
);
}

@Test
public void testLookupWithNestedFieldArray() throws Exception {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'author.id', foreignField: '_id', as: 'author'");
configureAuthorsCollection("_id: 3", "_id: 3, name: 'Uncle Bob'");
configureAuthorsCollection("_id: 4", "_id: 4, name: 'Aunt Bobby'");
Document document = json("title: 'Clean Code', author: [{ id: 3 }, {id: 4}]");

Stream<Document> result = lookupStage.apply(Stream.of(document));

assertThat(result).containsExactly(
json("title: 'Clean Code', author: { id: 3 }, author: [{_id: 3, name: 'Uncle Bob'}, {_id: 4, name: 'Aunt Bobby'}]")
);
}

private LookupStage buildLookupStage(String jsonDocument) {
return new LookupStage(json(jsonDocument), database);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,93 @@ void testAggregateWithLookupAndUncorrelatedSubqueries() {
);
}

@Test
void testLookupPersonWithSubscription() {
MongoCollection<Document> personCollection = db.getCollection("Person");
MongoCollection<Document> subscriptionCollection = db.getCollection("Subscription");

personCollection.insertOne(json("""
"_id": 666,
"personId": 666,
"Relation": [
{
"organizationId": "org1",
"active": true
},
{
"organizationId": "org3",
"active": false
}
]
"""));

subscriptionCollection.insertOne(json("""
"_id": 123,
"organizationId": "org1",
"Subscription": [
{
"Id": 1,
"Name": "Netflix"
},
{
"Id": 12,
"Name": "Disney+"
}
]
"""));
subscriptionCollection.insertOne(json("""
"_id": 456,
"organizationId": "org2",
"Subscription": [
{
"Id": 2,
"Name": "HBO Max"
},
{
"Id": 11,
"Name": "SkyShowtime"
}
]
"""));

List<Document> pipeline = jsonList(
"$match: { 'personId': 666 }",
"$lookup: {from: 'Subscription', localField: 'Relation.organizationId', foreignField: 'organizationId', as: 'Subscriptions'}"
);

assertThat(personCollection.aggregate(pipeline))
.containsExactly(json("""
"_id": 666,
"personId": 666,
"Relation": [
{
"organizationId": "org1",
"active": true
},
{
"organizationId": "org3",
"active": false
}
],
"Subscriptions": [
{
"_id": 123,
"organizationId": "org1",
"Subscription": [
{
"Id": 1,
"Name": "Netflix"
},
{
"Id": 12,
"Name": "Disney+"
}
]
}
]
"""));
}

@Test
void testAggregateWithReplaceRoot() {
List<Document> pipeline = jsonList("$replaceRoot: { newRoot: '$a.b' }");
Expand Down