【发布时间】:2014-02-23 11:03:00
【问题描述】:
我需要维护一个将数据存储在 MongoDB 中的应用程序,而且我对 mapReduce 语法很流利。
我想做一个 mapReduce 操作,其中发出的键是 DBRef 的 ID,而不是它的值。我从MongoDB documentation 清楚地了解到,出于并行性原因,一个人不能也不能访问数据库。
所以我的收藏文档是这样的:
{
_id: "66f072b8-2422-4022-826b-b20b832a1ee6",
_class: "com.foo.bar",
foo_bar: 1,
user: {
"$dbref": {
namespace: "user",
oid: "6cd0dac5-a511-48b1-b437-318ad74061a5"
}
}
}
当前的 mapReduce 是这样的:
db.myCollection.mapReduce(
function () {
if (this.foo_bar > 0) {
emit(this.user, this.foo_bar);
}
},
function (key, values) {
var sum = 0;
for (var i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
},
{
"out" : { "inline" : 1}
}
)
有很多文档,AWS 和 MongoHQ 之间的加载需要很长时间。我希望能够使用 user$id 作为键,而不是 DBRef 的文档。
我已经测试了以下但没有成功:
- emit(this.user.id, this.foo_bar);
- emit(this.user.oid, this.foo_bar);
- emit(this.user._id, this.foo_bar);
- emit(this.user.dbref.id,this.foo_bar);
- emit(this.user.dbref.oid, this.foo_bar);
- emit(this.user.dbref._id, this.foo_bar);
- emit(this.user['$dbref'].oid, this.foo_bar); (根据 cmets 中的 Sammaye 建议)
正确的语法是什么?
【问题讨论】:
-
this.user['$dbref'].oid -
不。 Mongo 给出:Thu Jan 30 23:22:52.584 map reduce failed:{ "errmsg" : "exception: TypeError: Cannot read property 'oid' of undefined near '['$dbref'].oid, this.foo_bar )' (line 3)", "code" : 16722, "ok" : 0 } at src/mongo/shell/collection.js:970 - 有什么建议@cubitouch?
-
我认为这里最大的问题是Java驱动程序没有标准化他们的dbrefs,我假设你从文档的其他细节中使用java
-
我们确实使用 Java (Spring MongoDB)。我给你的输出是在 OSX 上使用 mongo cli。
-
挂在 Ima 测试这个,没有解释为什么这不应该工作