I am trying to build the aggregate pipeline and somewhere it is causing an issue. To debug the issue, I’m trying to print the MongoDB query in JSON. Below is the syntax of code and it is causing an issue JSON reader was expecting a value but found ‘Document
Can anyone help me to print the query in JSON to debug the issue.Thanks in advance.
List<? extends Bson> pipeline = CreatePipeline(period, finPeriod, system);
BsonArray pipeline = new BsonArrayCodec().decode(new JsonReader(pipeline.toString()), DecoderContext.builder().build());
LOG.info("pipeline: " + pipeline );
private List<? extends Bson> CreatePipeline(String period, String finPeriod, String system){
return Arrays.asList(
new Document()
.append("$match", new Document()
.append("key", "NN")
.append("Type.key", "HH")
.append("Period", finPeriod)
),
new Document()
.append("$addFields", new Document()
.append("GenerateRecordID", "$_id")
.append("Period", nextPeriod)
.append("amount", new Document()
.append("$cond", new Document()
.append("if", new Document()
.append("$ne", Arrays.asList(
"$key",
"YY"
)
)
)
.append("then", new Document()
.append("$multiply", Arrays.asList(
"$amount",
Decimal128.parse("-1.0")
)
))
.append("else", "$$REMOVE")
)
)
),
new Document()
.append("$project", new Document()
.append("_id", 0.0)
.append("Amt", new Document()
.append("$cond", new Document()
.append("if", new Document()
.append("$ne", Arrays.asList(
"$key",
"YY"
)
)
)
.append("then", 0.0)
.append("else", "$$REMOVE")
)
)
.append("Amount", new Document()
.append("$cond", new Document()
.append("if", new Document()
.append("$ne", Arrays.asList(
"$key",
"BB"
)
)
)
.append("then", 0.0)
.append("else", "$$REMOVE")
)
)
new Document()
.append("$merge", new Document().append("into",new Document().append("db",this.database).append("coll",sourceSystem + "Temp")))
);
}