我有四个收藏品
1.links(movieId,imdbId,tmdbId)
2.movies(movieId,title,genres),
3.tags(userId,movieId,tag,timestamp),
4.ratings(userId,movieId,rating,timestamp).
现在我需要做的是将它们聚合成一个集合,如下所示
movieId,imdbId,tmdbId,
title,genres,
u_data{[
{userId,tag,tag_timestamp,
rating,rating_timestamp}
]}
我怎样才能实现这一点。
我提到link1。但无法得出答案。 (我使用dataset from grouplens.org)
答案 0 :(得分:0)
为简洁起见,我合并了你的前两个系列。此外,因为他们建议一对一的关系,所以如果16 MB的限制不是您的情况下的问题,那么建议将其保存在一个单独的文档中.....
db.t1.insert({
movieId:1,
title:"title-1",
generes:["g1", "g2"],
imdbId:111,
tmdbId:112
});
db.t2.insert({
movieId:1,
userId:11,
tag:"t1",
timestamp: new Date()
});
db.t2.insert({
movieId:1,
userId:12,
tag:"t2",
timestamp: new Date()
});
db.t3.insert({
movieId:1,
userId:11,
rating:2,
timestamp: new Date()
});
db.t3.insert({
movieId:1,
userId:12,
rating:3,
timestamp: new Date()
});
db.t1.find().forEach(function(e){
var mId = e.movieId;
var arr = [];
var finalOutput = {};
finalOutput.movieId = mId;
finalOutput.title = e.title;
finalOutput.generes = e.generes;
finalOutput.imdbId = e.imdbId;
finalOutput.tmdbId = e.tmdbId;
db.t2.find({"movieId":mId}).forEach(function(e2){
var v = {};
v.userId = e2.userId;
v.tag = e2.tag;
v.tag_ts = e2.timestamp;
arr.push(v);
});
for(i in arr){
db.t3.find({movieId:mId, userId:arr[i].userId}).forEach(function(e3){
arr[i].rating = e3.rating;
arr[i].rating_ts =e3.timestamp;
});
}
finalOutput.u_data = arr;
printjson(finalOutput);
});
输出:https://www.dropbox.com/s/ijwg2xtbrwa8782/Screenshot%202015-06-22%2000.49.07.png?dl=0