如何在map reduce工作的输出中添加集合名称,以获取集合名称.my代码的集合中的所有键

时间:2013-08-02 00:42:44

标签: mapreduce mongo-collection

var allCollections = db.getCollectionNames(); 
for (var i = 0; i < allCollections.length; ++i) { 
  var collectioname = allCollections[i];
  if (collectioname === 'system.indexes') 
     continue; 
  db.runCommand(
        { "mapreduce" : collectioname, "map" : function() 
                              { 
                                 for (var key in this) { 
                                   emit(key, null); 
                                 } 
                              }, "reduce" : function(key, stuff) { 
                                      return null; 
                                 }, "out":mongo_test + "_keys" 
                              }) } 



output

{ "_id" : "_id", "value" : null }
{ "_id" : "collection_name", "value" : null }
{ "_id" : "database", "value" : null }
{ "_id" : "host", "value" : null }
{ "_id" : "port", "value" : null }
{ "_id" : "cardid", "value" : null }
{ "_id" : "ccard", "value" : null }
{ "_id" : "creditcardnum", "value" : null }
{ "_id" : "date", "value" : null }
{ "_id" : "value", "value" : null } 

我想在“值”字段中使用collectionname而不是null。

0 个答案:

没有答案