简体   繁体   中英

Mongoose populate documents

I got 3 database models in mongoose that looks like this:

//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}],
});

And I try to populate a profile with

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

The matches get populated but I dont get the scores in matches to populate. Is this not supported in mongoose or do I do something wrong? Populate gives me this:

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

But I want to populate the score array in the match array. Can I do this?

Yes, you are right. I tried using Chaining of populate I got same output.

For your query please use async.js and then populate by the method mentioned below.

For more details please have a look at this code snippet . It is a working, tested, sample code according to your query. Please go through the commented code for better understanding in the code below and the link of the snippet provided.

//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);
            })
        })
    }
});
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
        .populate('matches.scores')
        .exec(function(err, profile) {
            if (err) {...}
            if (profile) {
               console.log(profile);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM