mongoose js填充数组中的嵌入对象

时间:2015-07-04 08:31:20

标签: node.js mongodb mongoose

我正在尝试显示与训练事件关联的主题名称列表,但是对象id是查询执行后填充的唯一属性。我已尝试过这个QUESTION中的解决方案,它似乎与我想要实现的类似,但它无法正常工作。 我的加载方法位于此代码段的最底部。

感谢任何帮助,谢谢。

var UserSchema = new Schema({
    firstName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your first name']
    },
    lastName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your last name']
    },
    displayName: {
        type: String,
        trim: true
    }
});


var SubjectSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill subject name',
        trim: true
    },
    description: {
        type: String,
        default: '',
        required: 'Please fill subject description',
        trim: true
    }
});

var TrainingSubjectSchema = new Schema({
    subject: {
        type: Schema.ObjectId,
        ref: 'Subject'
    },
    attendanceRate:[{
        type: Number
    }]
});


var TrainingEventSchema = new Schema({
    name: {
        type: String,
        default: '',
        required: 'Please fill training event name',
        trim: true
    },
    created: {
        type: Date,
        default: Date.now
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    },
    attendees:[{
        type: Schema.ObjectId,
        ref: 'User'
    }],
    subjects:[TrainingSubjectSchema]
});


// retrieve method
Training.findById(id).populate('user', 'displayName').populate('subjects.subject','name').exec(function(err, trainingevent)

1 个答案:

答案 0 :(得分:1)

不确定为什么它不适合你,但我已经尝试过了 在我的代码中,它按预期工作。

这就是我创建模式的方式:

    var async = require('async');
    var models = require('./models.js');
    var User = models.User;
    var Subject = models.Subject;
    var TrainingSubject = models.TrainingSubject;
    var TrainingEvent = models.TrainingEvent;

    async.waterfall(
      [
        clearDB.bind(null, {collections: [User, Subject, TrainingSubject, TrainingEvent], async: async}),
        createUsers.bind(null, {User: User, async: async}),
        createSubjects.bind(null, {async: async, Subject: Subject}),
        createTrainingEvents.bind(null, {async: async, TrainingEvent: TrainingEvent}),
        showTrainingEvents.bind(null, {TrainingEvent: TrainingEvent})
      ]
    );

    function createTrainingEvents(data, db, next) {
      var firstSubject = db.subjects[0];
      var secondSubject = db.subjects[1];

      var trainingSubjects = [
        {
          subject: firstSubject._id,
          attendanceRate: [5, 5]
        },
        {
          subject: secondSubject._id,
          attendanceRate: [4, 4, 5]
        }
      ];

      var trainingEvents = [
        {
          name: 'October Fest',
          user: db.users[0]._id,
          subjects: [trainingSubjects[0]],
          attendes: [db.users[0]._id, db.users[1]._id]
        },
        {
          name: 'August Fest',
          user: db.users[1]._id,
          subjects: [trainingSubjects[1]],
          attendes: [db.users[0]._id, db.users[1]._id]
        }
      ];

      data.async.map(
        trainingEvents,
        function(trainingEvent, done) {
          data.TrainingEvent.create(trainingEvent, done);
        },
        function(err, result) {
          next(err);
        }
      );
    }

    function clearDB(data, next) {
      async.map(
        data.collections,
        function(collection, done) {
          collection.remove({}, done);
        },
        function(err) {
          next(err);
        }
      );
    }

    function createUsers(data, next) {
      var users = [
        {firstName: 'Wilson', lastName: 'Balderrama', displayName: 'Wily'},
        {firstName: 'Santiago', lastName: 'Balderrama', displayName: 'Santi'},
        {firstName: 'Keila', lastName: 'Balderrama', displayName: 'Kei'}
      ];

      data.async.map(
        users,
        function(user, done) {
          data.User.create(user, done);
        },
        function(err, results) {
          next(err, {users: results});
        }
      );
    }

    function showUsers(data, next) {
      data.User.find({}, function(err, users) {
        next();
      });
    }

    function createSubjects(data, db, next) {
      var subjects = [
        {
          name: 'JavaScript for Beginners',
          description: 'JS fundamentals'
        },
        {
          name: 'Node.JS for Beginners',
          description: 'Node.JS streams'
        }
      ];

      data.async.map(
        subjects,
        function(subject, done) {
          data.Subject.create(subject, done);
        },
        function(err, result) {
          db.subjects = result;
          next(err, db);
        }
      );
    }

    function createTrainingSubjects(data, db, next) {
      var firstSubject = db.subjects[0];
      var secondSubject = db.subjects[1];

      var trainingSubjects = [
        {
          subject: firstSubject._id,
          attendanceRate: [5, 5]
        },
        {
          subject: secondSubject._id,
          attendanceRate: [4, 4, 5]
        }
      ];


      data.async.map(
        trainingSubjects,
        function(trainingSubject, done) {
          data.TrainingSubject.create(trainingSubject, done);
        },
        function(err, result) {
          db.trainingSubjects = result;
          next(err, db);
        }
      );
    }

    function showTrainingSubjects(data, next) {
      data.TrainingSubject
        .find({})
        .populate('subject')
        .exec(function(err, data) {
          next();
        });
    }

    function showSubjects(data, next) {
      data.Subject.find({}, function(err, data) {
        next();
      });
    }

    function showTrainingEvents(data, next) {
      data.TrainingEvent
        .find({name: 'August Fest'})
        .populate('user', 'displayName')
        .populate('subjects.subject', 'name')
        .exec(function(err, data) {
          console.log(JSON.stringify(data));
          next();
        });
    }

在这里,我正在使用模型处理数据:

    [
       {
          "_id":"55981d58cfd48c905c3a5fde",
          "user":{
             "_id":"55981d58cfd48c905c3a5fd8",
             "displayName":"Santi"
          },
          "__v":0,
          "subjects":[
             {
                "subject":{
                   "_id":"55981d58cfd48c905c3a5fdb",
                   "name":"Node.JS for Beginners"
                },
                "_id":"55981d58cfd48c905c3a5fdf",
                "attendanceRate":[
                   4,
                   4,
                   5
                ]
             }
          ],
          "attendes":[
             "55981d58cfd48c905c3a5fd7",
             "55981d58cfd48c905c3a5fd8"
          ],
          "created":"2015-0704T17:52:24.181Z",
          "name":"August Fest"
       }
    ]

输出是;

func Collision(Circle: SKSpriteNode, Triangle: SKSpriteNode) {
        var mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var vc = mainStoryboard.instantiateViewControllerWithIdentifier("1") as! UIViewController
        self.view!.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
}