Mongoose saving for populate
<p>I'm new to Mongoose and Nodejs developement in general and I've got a bit of confusion around how to properly set up saving my records. Here are my two schemas:</p>

<p><strong>Download</strong></p>

<pre><code>var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var downloadSchema = Schema({
    title        : String,
    description  : String,
    _project      : { type: Schema.Types.ObjectId, ref: 'Project' }
});

module.exports = mongoose.model('Download', downloadSchema);
</code></pre>

<p><strong>Project</strong></p>

<pre><code>...

var projectSchema = Schema({
    name         : String,
    url          : String,
    pwd          : String,
    _downloads   : [{type: Schema.Types.ObjectId, ref: 'Download' }]
});

module.exports = mongoose.model('Project', projectSchema);
</code></pre>

<p>This appears to be working correctly. The documentation explains my use-case of saving a download and linking a project, but I'm not sure how to properly populate the <code>Project._downloads</code>. Here's what I've done:</p>

<p><strong>Express route handler:</strong></p>

<pre><code>function createDownload(req, res) {

    // the Project Id is passed in the req.body as ._project
    var dldata = req.body;

    Project.findOne({ _id : dldata._project }, function(err, project) {
        var dload = new Download(dldata);


        dload.save( function (err, download) {

            project._downloads.push(download._id);
            project.save( function(err){
                var msg = {};
                if(err) {
                    msg.status = 'error';
                    msg.text = err;
                }else {
                    msg.status = 'success';
                    msg.text = 'Download created successfully!';
                }

                res.json(msg);
            });

        });
    });
}
</code></pre>

<p>This seems overcomplicated to me. Am I supposed to be manually <code>push</code>ing to the <code>._downloads</code> array, or is that something Mongoose is supposed to handle internally based on the schema? Is there a better way to achieve it so that I can do:</p>

<pre><code>Download.find().populate('_project').exec( ...
</code></pre>

<p>as well as:</p>

<pre><code>Project.findOne({_id : _projectId}).populate('_downloads').exec( ...
</code></pre>