2

_idすべてのドキュメントが同じタイプでフィールド数が異なると仮定して、MongoDB の 1 つのコレクション内のすべてのドキュメントに存在するフィールドのスーパー セット ( を除く) を作成する方法。

例 :

doc1 - {"_id":"test1", "firstName":"sample1", "age":24, "state":"Kansas"}
doc2 - {"_id":"test2", "lastName":"sample2", "age":24, "country":"US"}

スーパーセットは次のとおりです。{"firstName", "lastName", "age", "state", "country"}

4

1 に答える 1

2

入力コレクション内のすべての指定されたドキュメント キーを結果ドキュメントのキーとして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");
于 2016-08-05T13:26:50.537 に答える