【问题标题】:mongodb get list of fields in all collections by using map reduce jobmongodb使用map reduce作业获取所有集合中的字段列表
【发布时间】:2013-08-01 15:34:55
【问题描述】:

我在 mongo 数据库中有一个集合名称列表。我需要一个脚本,通过在 mongo 数据库中使用 map reduce 作业将集合名称作为参数传递来获取每个集合中的所有文件名称。

这是我目前所拥有的:

mr = db.runCommand({
    "mapreduce" : "collectionname",
    "map" : function() { for (var key in this) { emit(key, null); } },
    "reduce" : function(key, stuff) { return null; },
    "out": "collectioname" + "_keys"
})

或者在 mongo shell 中执行它的一行代码:

mr = db.runCommand({ "mapreduce" : "collectionname", "map" : function() { for (var key in this) { emit(key, null); } }, "reduce" : function(key, stuff) { return null; }, "out": "collectioname" + "_keys" })

此命令用于获取集合中的字段列表。但是这个只适用于初级。我需要循环它(获取数据库中每个集合中的所有字段)。非常感谢。

【问题讨论】:

  • 我们不在你的指挥之下。向我们展示您目前的成果,我们会尽力为您提供帮助。
  • mr = db.runCommand({ "mapreduce" : "collectionname", "map" : function() { for (var key in this) { emit(key, null); } }, " reduce" : function(key, stuff) { return null; }, "out": "collectioname" + "_keys" }) 这是我必须用来获取集合中字段列表的命令。但是这个正在工作只在主要的。我需要循环它来获取数据库中每个集合中的所有字段
  • 这个问题已经回答了好几次了,其实我自己也回答过3次了,你可以看看:github.com/variety/variety
  • 如果主机有用户名 pwd 和端口值,如何运行此代码。我已尝试如下从此处复制代码variety.js。但以下命令返回错误。MongoDB shell 版本:2.0-A3连接到:ma-ireconm-ldb02.corp.apple.com:10901/admin Tue Jul 30 21:50:48 SyntaxError: syntax error (shell eval):1 mongo -u $usernames -p $PWD $HOST:$PORT /admin --eval "db = db.getSiblingDB('database')","var collection = 'collectioname'"variety.js
  • 您能正确格式化您的代码 sn-ps 吗?这太棒了

标签: mongodb mapreduce


【解决方案1】:

您要查找的 for 循环是:

var allCollections = db.getCollectionNames();

for (var i = 0; i < allCollections.length; ++i) {
     var collectioname = allCollections[i];
     // now do your map reduce for one collection here
     // there are the merge or reduce options for the output collection
     // in case you want to gather everything in one collection instead of:
     // collectioname + '_keys'
}

将此保存在script.js

然后运行它:

 mongo myDb script.js

或者写成一行然后在mongo shell中执行:

var allCollections = db.getCollectionNames(); for (var i = 0; i &lt; 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": collectioname + "_keys" }) }

但是,请彻底浏览mapReduce 页面并在其中运行ALL 示例。这应该清楚地说明如何使用emit 函数在最后获得正确的结果。您当前为某个键发出 null。那里和 reduce 的返回是您应该使用一些数据并聚合您想要的值的地方(例如集合名称,这将是一个被传递的常量)。

【讨论】:

  • 嗨 Gabriel Petrovay,感谢您的重播。我是 mongodb 的新手。我想了解如何在循环中编写 map reduce 并获取数据库中的所有集合键并使用集合名称加载到临时集合中和输出中的键。非常感谢
  • 你的问题是。你所描述的就是你在上面看到的:for 循环,你只需要将你的 map reduce 放在里面。什么不起作用?
  • 能否请验证此代码 var allCollections = db.getCollectionNames(); for (var i = 0; i
  • var allCollections = db.getCollectionNames(); rs_aissii4t_shard_1it:PRIMARY> for (var i = 0; i 映射:function() {for (var key in this) { emit(键,空); } ... , ... reduce : function(key, stuff) { return null; } ... out: "collectioname" + "_keys"} Wed Jul 31 17:03:34 SyntaxError: syntax error (shell):2 你能帮我解决这个语法错误吗?
  • 我在上面的答案中添加了一个对我有用的衬线。我将system.indexes 过滤掉了,因为:1. 当你想对它进行 mapreduce 时它会抛出一个错误 2. 你可能对它的内容不感兴趣,因为它不是用户数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
相关资源
最近更新 更多