How to add collection name in the output of map reduce job to get all the keys in the collection with collection name .my code is like

拜拜、爱过 提交于 2019-12-12 02:53:41

问题


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 } 

I want collectionname in the "value" field instead of null .

来源:https://stackoverflow.com/questions/18006980/how-to-add-collection-name-in-the-output-of-map-reduce-job-to-get-all-the-keys-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!