Skip to content

Commit

Permalink
add a test for a new(?) example on $unionWith
Browse files Browse the repository at this point in the history
add support for invoking without a collection name or type
  • Loading branch information
evanchooly committed May 13, 2024
1 parent 5de5268 commit 07830d9
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ public Class<UnionWith> getEncoderClass() {
@Override
protected void encodeStage(BsonWriter writer, UnionWith unionWith, EncoderContext encoderContext) {
String name = unionWith.collectionName();
String collectionName = name != null ? name
: getDatastore().getMapper().getEntityModel(unionWith.collectionType()).collectionName();
Class<?> type = unionWith.collectionType();
final String collectionName;
if (name != null) {
collectionName = name;
} else if (type != null) {
collectionName = getDatastore().getMapper().getEntityModel(type).collectionName();
} else {
collectionName = null;
}

if (unionWith.pipeline().isEmpty()) {
writer.writeString(collectionName);
} else {
document(writer, () -> {
value(writer, "coll", collectionName);
if (collectionName != null) {
value(writer, "coll", collectionName);
}
value(getDatastore().getCodecRegistry(), writer, "pipeline", unionWith.pipeline(), encoderContext);
});
}
Expand Down
32 changes: 31 additions & 1 deletion core/src/main/java/dev/morphia/aggregation/stages/UnionWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public class UnionWith extends Stage {
private Class<?> collectionType;
private String collectionName;

/**
* Creates the new stage
*
* @param pipeline the pipeline
* @hidden
* @morphia.internal
*/
@MorphiaInternal
private UnionWith(List<Stage> pipeline) {
super("$unionWith");
this.pipeline = Collections.unmodifiableList(pipeline);
}

/**
* Creates the new stage
*
Expand Down Expand Up @@ -54,13 +67,29 @@ private UnionWith(Class<?> type, List<Stage> pipeline) {
* Performs a union of two collections; i.e. $unionWith combines pipeline results from two collections into a single result set. The
* stage outputs the combined result set (including duplicates) to the next stage.
*
* @param type the type to perform the pipeline against
* @param stages the pipeline stages
*
* @return the new Stage
*
* @aggregation.stage $unionWith
* @mongodb.server.release 4.4
* @since 3.0
*/
public static Stage unionWith(Stage... stages) {
return new UnionWith(Expressions.toList(stages));
}

/**
* Performs a union of two collections; i.e. $unionWith combines pipeline results from two collections into a single result set. The
* stage outputs the combined result set (including duplicates) to the next stage.
*
* @param type the type to perform the pipeline against
* @param stages the pipeline stages
*
* @return the new Stage
*
* @aggregation.stage $unionWith
* @mongodb.server.release 4.4
*/
public static Stage unionWith(Class<?> type, Stage... stages) {
return new UnionWith(type, Expressions.toList(stages));
Expand Down Expand Up @@ -98,6 +127,7 @@ public String collectionName() {
* @hidden
* @morphia.internal
*/
@Nullable
@MorphiaInternal
public Class<?> collectionType() {
return collectionType;
Expand Down
3 changes: 3 additions & 0 deletions core/src/test/java/dev/morphia/test/TemplatedTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ protected List<Document> loadJson(String name, String type, boolean failOnMissin
while (reader.ready()) {
String json = reader.readLine();
try {
if (json.startsWith("[") && json.endsWith("]")) {
json = json.substring(1, json.length() - 1);
}
data.add(Document.parse(json));
} catch (JsonParseException e) {
throw new JsonParseException(e.getMessage() + "\n" + json, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.testng.annotations.Test;

import static dev.morphia.aggregation.expressions.AccumulatorExpressions.sum;
import static dev.morphia.aggregation.expressions.Expressions.document;
import static dev.morphia.aggregation.stages.Documents.documents;
import static dev.morphia.aggregation.stages.Group.group;
import static dev.morphia.aggregation.stages.Group.id;
import static dev.morphia.aggregation.stages.Set.set;
Expand Down Expand Up @@ -42,4 +44,17 @@ public void testExample2() {
.field("total", sum("$quantity")),
sort().descending("total")));
}

@Test
public void testExample3() {
testPipeline(ServerVersion.ANY, false, true, (aggregation) -> aggregation.pipeline(
unionWith(
documents(
document("_id", 4)
.field("flavor", "orange"),
document("_id", 5)
.field("flavor", "vanilla")
.field("price", 20)))));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ _id: 1, flavor: "chocolate" },
{ _id: 2, flavor: "strawberry" },
{ _id: 3, flavor: "cherry" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ _id: 1, flavor: 'chocolate' },
{ _id: 2, flavor: 'strawberry' },
{ _id: 3, flavor: 'cherry' },
{ _id: 4, flavor: 'orange' },
{ _id: 5, flavor: 'vanilla', price: 20 }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create a Union with Specified Documents
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
$unionWith: {
pipeline: [
{
$documents: [
{ _id: 4, flavor: "orange" },
{ _id: 5, flavor: "vanilla", price: 20 }
]
}
]
}
}

0 comments on commit 07830d9

Please sign in to comment.