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

2
  • 1
    Can you post your documents , expected results and what you have tried in mongo script? Commented May 19, 2022 at 1:12
  • Do you need to get the schema via code? MongoDB Compass will do it for you: mongodb.com/docs/compass/current/schema Commented Sep 10, 2022 at 19:45

0

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.