0

I am using the Java MongoDB driver v4.3:

implementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.3.1'
implementation group: 'org.mongodb', name: 'bson', version: '4.3.1'

I have my aggregation pipelines written in JSON files which are placed in the src/main/resources folder. The aggregate function only accepts a List<Bson>. After fetching the file, how do I pass it into the MongoDB method?

String fileName = "physics/test.json";
File file = new File(fileName);
MongoDatabase db = mongoClient.getDatabase(DATABASE_NAME);
MongoCollection collection = db.getCollection(collectionName);
// Convert file to List<Bson> ???
AggregateIterable sourceList = collection.aggregate(pipeline);
5
  • I don't believe there are any converters that will convert JSON to Java-based aggregation pipelines. In Atlas, there is a code converter, but this is expected to be a design-time implementation, not a run-time implementation. Commented Aug 19, 2021 at 17:39
  • The aggregate pipeline is an array of aggregation stages - and in Java this is a List of stages - each stage defined as Bson or its implementation org.bson.Document. You may want to tell what the contents of the file look like. Maybe you can read the file and convert it into the List. Commented Aug 20, 2021 at 3:30
  • @prasad_ I have many files which have aggregations that consists of lots of pipelines. One pipeline is like this: [{"$group":{"_id":{"segment":"$segment","invoice_id":{"$trim":{"input":"$invoice_id"}}},"qty":{"$sum":"$qty"}}}] Commented Aug 20, 2021 at 9:12
  • Thats native code (and its a JSON string). You may have to parse it to instances of Bson. The Document class has some methods like parse. Commented Aug 20, 2021 at 9:49
  • @prasad_ How do I do that? Commented Aug 20, 2021 at 11:24

1 Answer 1

1

This is how you do it:

import org.bson.json.JsonObject;
// ...
    
String json = """
        [{"$group":{"_id":{"segment":"$segment","invoice_id":{"$trim":{"input":"$invoice_id"}}},"qty":{"$sum":"$qty"}}}]""";

List<BsonDocument> pipeline = new BsonArrayCodec().decode(new JsonReader(json), DecoderContext.builder().build())
        .stream().map(BsonValue::asDocument)
        .collect(Collectors.toList());

MongoClient client = new MongoClient();

List<JsonObject> results = client.getDatabase("test").getCollection("test").withDocumentClass(JsonObject.class)
        .aggregate(pipeline).into(new ArrayList<>());

for (JsonObject cur: results) {
    System.out.println(cur.getJson());
}
Sign up to request clarification or add additional context in comments.

1 Comment

This will fail in case the JSON contains datetimes, because there are treated like strings not ISODate(). And it will fail if you are using constants like ISODate() because this is not a valid JSON anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.