mongodb mapreduce:发射dbref oid作为钥匙



我需要维护一个存储在mongodb中的数据,而我会使用MapReduce语法流利。

我想制作一个mapReduce操作,其中发射的密钥是 dbref id ,而不是值。我从MongoDB文档中清楚地了解,出于并行性原因,一个人必须也不能访问DB。

所以我的收集文档就是这样:

{
  _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之间的负载花费了很多时间。我希望能够将用户$ id用作键,而不是dbref的文档。

我已经测试了以下测试,但没有成功:

  1. emit( this.user.id ,this.foo_bar);
  2. emit( this.user.oid ,this.foo_bar);
  3. emit( this.user._id ,this.foo_bar);
  4. emit( this.user.dbref.id ,this.foo_bar);
  5. emit( this.user.dbref.oid ,this.foo_bar);
  6. emit( this.user.dbref._id ,this.foo_bar);
  7. emit( this.user ['$ dbref']。oid ,this.foo_bar);(根据评论中的sammaye建议)

正确的语法是什么?

我相信这里的真正答案是重新访问您的模式设计。据注意,弹簧数据方法更偏向传统的关系设计,而不是动态文档结构MongoDB提供的。

也就是说,这是一个链接,其中包括MongoDB社区经理的更多解释和替代方案。遵循该响应中的注释和链接。

重新设计以外的唯一方法,这实际上只是实际执行重新设计的解决方法是 pre-join 使用 output 的数据从地图减少。这是一个增量步骤。结果集合将允许您查询子插件的属性,而无需将每个文档从电线上拉下来。

作为另一个注意事项,将类似的输出选项设置为在MongoDB的下一个版本中添加到聚合框架管道中。

最新更新