MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

Populate() ref imbriqué dans le tableau d'objets

Un peu de code peut-être, aussi quelques corrections à votre approche. Vous voulez en quelque sorte un type de jointure "manyToMany" que vous pouvez faire comme suit :

var async = require("async"),
    mongoose = require("mongoose"),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/user');


var userSchema = new Schema({
  email: String,
  displayName: String,
  subscriptions: [{ type: Schema.Types.ObjectId, ref: 'UserShow' }]
});

userShows = new Schema({
  show: { type: Schema.Types.ObjectId, Ref: 'Show' },
  favorite: { type: Boolean, default: false }
});

var showSchema = new Schema({
  title: String,
  overview: String,
  subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  episodes: [{
    title: String,
    firstAired: Date
  }]
});


var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);
var UserShow = mongoose.model('UserShow', userShows);

var user = new User({
  email: '[email protected]',
  displayName: 'bill'
});

user.save(function(err,user) {

  var show = new Show({
    title: "Some Show",
    overview: "A show about some stuff."
  });

  show.subscribers.push( user._id );
  show.save(function(err,show) {
    var userShow = new UserShow({ show: show._id });
    user.subscriptions.push( userShow._id );
    userShow.save(function(err,userShow) {
      user.save(function(err,user) {
        console.log( "done" );
        User.findOne({ displayName: "bill" })
          .populate("subscriptions").exec(function(err,user) {

          async.forEach(user.subscriptions,function(subscription,callback) {
              Show.populate(
                subscription,
                { path: "show" },
              function(err,output) {
                if (err) throw err;
                callback();
              });

          },function(err) {
            console.log( JSON.stringify( user, undefined, 4) );
          });


        });
      });
    });
  });

});

Cela devrait afficher une réponse peuplée semblable à celle-ci :

{
    "_id": "53a7b8e60462281231f2aa18",
    "email": "[email protected]",
    "displayName": "bill",
    "__v": 1,
    "subscriptions": [
        {
            "_id": "53a7b8e60462281231f2aa1a",
            "show": {
                "_id": "53a7b8e60462281231f2aa19",
                "title": "Some Show",
                "overview": "A show about some stuff.",
                "__v": 0,
                "episodes": [],
                "subscribers": [
                    "53a7b8e60462281231f2aa18"
                ]
            },
            "__v": 0,
            "favorite": false
        }
    ]
}

Ou sans le "manyToMany" fonctionne aussi. Notez ici qu'il n'y a pas d'appel initial à remplir :

var async = require("async"),
    mongoose = require("mongoose"),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/user');


var userSchema = new Schema({
  email: String,
  displayName: String,
  subscriptions: [{
    show: {type: Schema.Types.ObjectId, ref: 'UserShow' },
    favorite: { type: Boolean, default: false }
  }]
});


var showSchema = new Schema({
  title: String,
  overview: String,
  subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  episodes: [{
    title: String,
    firstAired: Date
  }]
});


var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);

var user = new User({
  email: '[email protected]',
  displayName: 'bill'
});

user.save(function(err,user) {

  var show = new Show({
    title: "Some Show",
    overview: "A show about some stuff."
  });

  show.subscribers.push( user._id );
  show.save(function(err,show) {
    user.subscriptions.push({ show: show._id });
    user.save(function(err,user) {
        console.log( "done" );
        User.findOne({ displayName: "bill" }).exec(function(err,user) {

          async.forEach(user.subscriptions,function(subscription,callback) {
              Show.populate(
                subscription,
                { path: "show" },
              function(err,output) {
                if (err) throw err;
                callback();
              });

          },function(err) {
            console.log( JSON.stringify( user, undefined, 4) );
          });


        });
    });
  });

});