MongoDB Java使用聚合获取现有集合的架构
我正在寻找解决方案来获取所有嵌套字段的字段名称和类型,包括使用 $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" )))
));
我已经尝试实现但还无法处理文档数组。我正在尝试学习如何使用 $mergeObjects 来实现输出