【问题标题】:Mongoid map reduce in output collection to simulate join输出集合中的 Mongoid 映射减少以模拟连接
【发布时间】:2015-03-12 17:24:22
【问题描述】:

我正在使用 mongodb/mongoid,我在两个不同的集合中使用相同的键、值和返回格式运行两个 map/reduce 进程。

(就像在本教程中一样:http://tebros.com/2011/07/using-mongodb-mapreduce-to-join-2-collections/

我使用“out”选项来模拟两个输出之间的合并以模拟连接操作...

我的“加入”集合已被很好地填充,但只有当我迭代结果时!

如果我愿意

Model_A.collection.map_reduce(map_1, reduce).out(reduce: "my_collection) Model_B.collection.map_reduce(map_2, reduce).out(reduce: "my_collection)

不起作用,集合“my_collection”为空!

如果我愿意

res_A = Model_A.collection.map_reduce(map_1, reduce).out(reduce: "my_collection)

res_A.each 做|资源 | 放置 res.inspect 结束

res_B = Model_B.collection.map_reduce(map_1, reduce).out(reduce: "my_collection)

res_B.each 做 |资源 | 放置 res.inspect 结束

它正在工作,我的收藏“my_collection”很好地填充了“加入”值...

对于大型数据集,在 ruby​​ 中的 Web 应用程序服务器端进行迭代实在是太丑陋了……

有人遇到过这个问题吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails mongodb mapreduce mongoid


    【解决方案1】:

    对于第一个 map-reduce,您只需调用 #first,您的输出集合将填充所有值(实际上对于第二个 map-reduce,它也是如此,但您可能想在任何情况下迭代它的结果)。

    所以,而不是

    res_A.each do| res | puts res.inspect end
    

    只是做

    res_A.first
    

    您可以在 mongo 控制台中轻松检查集合是否已填充:

    db.my_collection.find()
    

    仅供参考,这是由于

    Model_A.collection.map_reduce(map_1, reduce).out(reduce: "my_collection)
    

    实际上不会触发 MongoDB 中的 db.collection.mapReduce() 函数。相反,它返回 Mongoid::Contextual::MapReduce 类的实例,您可以将其视为类似于 ActiveRecord::Relation 的东西。

    【讨论】:

    • 我怀疑这是返回“上下文”的东西,如果我可以用“第一”触发请求,那就完美了......我只是害怕在应用程序服务器上检索所有结果边。谢谢
    • welcome) 仅供参考,这里接受正确答案很常见
    猜你喜欢
    • 2012-05-30
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2018-09-07
    相关资源
    最近更新 更多