_id
假设所有文档的类型相同,但字段数不同,如何在MongoDB的单个集合中创建所有文档中存在的超大字段集(除外)。
例子 :
doc1 - {"_id":"test1", "firstName":"sample1", "age":24, "state":"Kansas"}
doc2 - {"_id":"test2", "lastName":"sample2", "age":24, "country":"US"}
超级集将是: {"firstName", "lastName", "age", "state", "country"}
您可以尝试运行mapReduce
将返回输入集合中所有给定文档键作为_id
结果文档键的操作。这将在输出集合中,然后您可以在其中应用_id
字段上的不同命令以检索字段的超集。
以下示例显示了此概念:
// Run mapReduce on collectionName
String map = "function () { for (var key in this) { emit(key, null); } }";
String reduce = "function () {}";
MapReduceResults<ValueObject> results = mongoTemplate.mapReduce(
"collectionName",
map,
reduce,
new MapReduceOptions().outputCollection("col_out"),
ValueObject.class
);
// Get the distinct keys from output collection col_out ---
List<String> fieldsSuperset = mongoTemplate.getCollection("col_out").distinct("_id");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句