MongoDB Java get Schema of existing collection using aggregation

234 Views Asked by At

I am looking for solutions to get field name and type for all nested fields including array of documents using $project $mergeobjects, $objectToArray , $unwind.

collection.aggregate(Arrays.asList(
                new Document("$limit",100),
                new Document("$project", new Document("fields", new Document("$objectToArray", "$ROOT" ))),
                new Document("$unwind", "$fields"),
                new Document("$project", new Document("fields", new Document("k", "$fields.k").append("v",new Document("$type", "$fields.v")))),
                new Document("$group", new Document("_id", "$fields.k").append("fields", new Document("$first", "$fields"))),
                new Document("$group", new Document("_id", null).append("fields", new Document("$push", "$fields"))),
                new Document("$project", new Document("fields", new Document("$arrayToObject", "$fields" )))
                ));

I have tried implementing but could not process array of documents yet. I am trying to learn how to use $mergeObjects to achieve the output

0

There are 0 best solutions below