【问题标题】:Mongoose populate documentsMongoose 填充文档
【发布时间】:2014-08-06 09:20:10
【问题描述】:

我在 mongoose 中有 3 个数据库模型,如下所示:

//profile.js
var ProfileSchema   = new Schema({
    username:    { type: String, required: true },                   
    password:    { type: String, required: true },                   
    matches:    [{ type: Schema.Types.ObjectId, ref: 'Match' }]
});

//match.js
var MatchSchema   = new Schema({ 
    scores:     [{ type: Schema.Types.ObjectId, ref: 'Score',  required: true }],
});

//score.js
var ScoreSchema   = new Schema({
    score:       {type: Number, required: true},
    achivement: [{type: String, required: true}],
});

我尝试用

填充个人资料
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
            .populate('matches')
            .populate('matches.scores')
            .exec(function(err, profile) {
                if (err) {...}
                if (profile) {
                   console.log(profile);
                }
            });

匹配被填充,但我没有得到匹配的分数来填充。猫鼬不支持这一点还是我做错了什么?填充给了我这个:

{
    user_token: "539b07397c045fc00efc8b84"
    username: "username002"
    sex: 0
    country: "SE"
    friends: []
    -matches: [
        -{
            __v: 1
            _id: "539eddf9eac17bb8185b950c"
            -scores: [
                "539ee1c876f274701e17c068"
                "539ee1c876f274701e17c069"
                "539ee1c876f274701e17c06a"
            ]
        }
    ]
}

但我想在匹配数组中填充分数数组。我可以这样做吗?

【问题讨论】:

    标签: javascript mongodb mongoose


    【解决方案1】:

    是的,你是对的。我尝试使用填充链我得到了相同的输出。

    对于您的查询,请使用async.js,然后通过下面提到的方法填充。

    更多详情请查看此代码snippet。根据您的查询,它是一个工作的、经过测试的示例代码。 请仔细阅读下面的代码和提供的sn-p链接中的注释代码以更好地理解。

    //Find the profile and populate all matches related to that profile
    Profile.findOne({
        _id: mongoose.Types.ObjectId(profile_id)
    })
    .populate('matches')
    .exec(function(err, profile) {
        if (err) throw err;
    
        //We got the profile and populated matches in Array Form
        if (profile) {
            // Now there are multiple Matches
            // We want to fetch score of each Match
            // for that particular profile
    
            // For each match related to that profile
            async.forEach(profile.matches, function(match) {
                console.log(match, 'match')
                // For each match related to that profile
                // Populate score achieved by that person
                Match.find({
                    _id:match.id
                })
                .populate('scores', 'score')
                .exec(function (err, score) {
                    if (err) throw err;
    
                    // here is score of all the matches
                    // played by the person whose profile id
                    // is passed
                    console.log(score);
                })
            })
        }
    });
    

    【讨论】:

      【解决方案2】:
      Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
              .populate('matches.scores')
              .exec(function(err, profile) {
                  if (err) {...}
                  if (profile) {
                     console.log(profile);
                  }
              });
      

      【讨论】:

      • 这篇文章被标记为低质量。请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
      猜你喜欢
      • 2017-01-27
      • 1970-01-01
      • 2014-08-16
      • 2017-03-21
      • 2017-06-06
      • 2018-08-16
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多