3

I have four collections

1.links(movieId,imdbId,tmdbId) 
2.movies(movieId,title,genres),
3.tags(userId,movieId,tag,timestamp), 
4.ratings(userId,movieId,rating,timestamp).

Now what i need to do is aggregate them into one collection as follows

movieId,imdbId,tmdbId,
title,genres,
u_data{[
{userId,tag,tag_timestamp,
rating,rating_timestamp}
]}

How can i achieve this.

I refered link1. But couldn't comeup with an answer. (i use dataset from grouplens.org)

Community
  • 1
  • 1

1 Answers1

0

For brevity, I merged your first two collections. Moreover as they suggest one to one relation, so its advisable to keep it in one single document if 16 MB limit is not the concern as in your case.....


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);
});

Output: https://www.dropbox.com/s/ijwg2xtbrwa8782/Screenshot%202015-06-22%2000.49.07.png?dl=0

Sachin Shukla
  • 173
  • 10