sql >> データベース >  >> NoSQL >> MongoDB

オブジェクト配列にネストされたPopulate()ref

    おそらくいくつかのコード、またあなたのアプローチへのいくつかの修正。次のように作成できる「manyToMany」タイプの結合が必要です。

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

    これにより、次のような入力済みの応答が表示されます。

    {
        "_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
            }
        ]
    }
    

    または、「manyToMany」がなくても機能します。ここでは、入力するための最初の呼び出しがないことに注意してください:

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


    1. phpとmongodbでfindAndModifyを使用する方法

    2. AzureMongoDBを使用するMeteor

    3. HerokuにMongoDBをデプロイする方法

    4. サブドキュメントの配列にあるフィールドのインデックス作成