gpt4 book ai didi

MongoDB 使用 MapReduce 交叉连接集合

转载 作者:行者123 更新时间:2023-12-05 08:00:31 24 4
gpt4 key购买 nike

我在 mongodb 中有 2 个集合

用户

{ "_id" : ObjectId("..."), "type" : "user", "user_id" : "U1" }
{ "_id" : ObjectId("..."), "type" : "user", "user_id" : "U2" }
{ "_id" : ObjectId("..."), "type" : "user", "user_id" : "U3" }

元素

{ "_id" : ObjectId("..."), "type" : "item", "item_id" : "I1" }
{ "_id" : ObjectId("..."), "type" : "item", "item_id" : "I2" }
{ "_id" : ObjectId("..."), "type" : "item", "item_id" : "I3" }
{ "_id" : ObjectId("..."), "type" : "item", "item_id" : "I4" }

我打算进行交叉连接以产生以下集合

用户项

{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I1", "user_id" : "U1", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I1", "user_id" : "U2", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I1", "user_id" : "U3", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I2", "user_id" : "U1", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I2", "user_id" : "U2", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I2", "user_id" : "U3", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I3", "user_id" : "U1", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I3", "user_id" : "U2", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I3", "user_id" : "U3", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I4", "user_id" : "U1", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I4", "user_id" : "U2", "score" : 0 }
{ "_id" : ObjectId("..."), "type" : "user_item", "item_id" : "I4", "user_id" : "U3", "score" : 0 }

我可以使用以下代码检索它

db.item.find().
forEach( function (i) {
db.user.find().
forEach( function (u) {
var row = {};
row.type = "user_item";
row.item_id = i.item_id;
row.user_id = u.user_id;
row.score = 0;
db.user_item.insert(row);
});
});

但问题是这种方法在处理大数据时非常慢(U=10,000,I = 10,000)。有没有办法在 mongodb 中使用 map-reduce 产生相同的输出,并且 map-reduce 会明显更快(理论上是的)?

注意:没有外键

最佳答案

您可以使用聚合和 $lookup 来完成

[{$lookup: 
{
from: 'item',
pipeline: [{$project: {_id:0, type:0}}],
as: 'item'
}},
{$unwind:
{
path: "$item"
}},
{$project: {type: "user_item",
item_id: "$item.item_id",
user_id:1
}},
{$set: {
score: 0
}}]

关于MongoDB 使用 MapReduce 交叉连接集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18163804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com