Advertisement
Guest User

Untitled

a guest
Mar 13th, 2015
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mongoose = require('mongoose'),
  2.     Schema = mongoose.Schema;
  3.  
  4. mongoose.connect('mongodb://localhost/mtc-test');
  5.  
  6. var youTubeSchema = Schema({
  7.     id: String,
  8.     _trailer: { type: Schema.Types.ObjectId, ref: 'Trailer' }
  9. });
  10.  
  11. var postersSchema = Schema({
  12.     posters: String,
  13.     _trailer : { type: Schema.Types.ObjectId, ref: 'Trailer' }
  14. });
  15.  
  16. var trailerSchema = Schema({
  17.     timestamp: String,
  18.     _youtube : { type: Schema.Types.ObjectId, ref: 'YouTube' },
  19.     _posters : { type: Schema.Types.ObjectId, ref: 'Posters' }
  20. });
  21.  
  22. var YouTube  = mongoose.model('YouTube', youTubeSchema);
  23. var Posters = mongoose.model('Posters', postersSchema);
  24. var Trailer = mongoose.model('Trailer', trailerSchema);
  25.  
  26. var trailer = new Trailer({
  27.     timestamp: new Date()
  28. });
  29.  
  30. var youtube = new YouTube({
  31.     id: "rTCxSguAmjQ",
  32.     _trailer: trailer._id
  33. });
  34.  
  35. var posters = new Posters({
  36.     posters: "4j3h34hg34ygyu34gkj43h.jpg",
  37.     _trailer: trailer._id
  38. });
  39.  
  40. trailer.save(function (err) {
  41.     //if (err) return handleError(err);
  42.  
  43.     youtube.save(function (err) {
  44.         //if (err) return handleError(err);
  45.     });
  46.  
  47.     posters.save(function (err) {
  48.         //if (err) return handleError(err);
  49.     });
  50. });
  51.  
  52. Trailer.update({_id: trailer._id}, {_youtube: youtube._id, _posters: posters._id}, {upsert: true}, function (err) {
  53.     //if (err) return handleError(err);
  54. });
  55.  
  56. Trailer
  57.     .findOne({ _id: trailer._id })
  58.     .populate('_youtube _posters')
  59.     .exec(function (err, trailer) {
  60.         //if (err) return handleError(err);
  61.         console.log(trailer);
  62.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement