0

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

varman
  • 8,704
  • 5
  • 19
  • 53
newbie
  • 1
  • 1
  • 1
    Can you post your documents , expected results and what you have tried in mongo script? – varman May 19 '22 at 01:12
  • Do you need to get the schema via code? MongoDB Compass will do it for you: https://www.mongodb.com/docs/compass/current/schema/ – Paul Sep 10 '22 at 19:45

0 Answers0