Mongoid map reduce in output collection 模拟连接

Mongoid map reduce in output collection to simulate join

我在两个不同的 collection 中使用 mongodb/mongoid 和我 运行 两个 map/reduce 进程,具有相同的键、值和 return 格式.

(如本教程中所示:http://tebros.com/2011/07/using-mongodb-mapreduce-to-join-2-collections/

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

我的 "joined" collection 填充得很好,但只有当我迭代结果时才会这样!

如果我这样做 :

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

不行,collection"my_collection"是空的!

如果我这样做 :

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

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

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

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

有效, 我的 collection "my_collection" 充满了 "joined" 值 ...

对于大数据集,在 ruby 中的 Web 应用程序服务器端迭代非常丑陋...

有人遇到过这个问题吗?

谢谢

对于第一个 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()函数。相反,它 returns 是 Mongoid::Contextual::MapReduce class 的一个实例,您可以将其视为类似于 ActiveRecord::Relation.

的东西