MongoDB: 未返回多个填充子文档

MongoDB: Multiple populate subdocuments not being returned

我正在尝试 return 文档和子文档的完整数据对象。 returning 是文档和子文档的 ObjectID。我试过多个填充语句,它也不起作用。我错过了什么?

用户架构

module.exports = function (connection) {
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    var user_fb = new mongoose.Schema({
        name: String,
        location: String,
        fb_id: Number,
        fb_image: String
    });

    return connection.model('User_Fb', user_fb);;
}

供稿架构

module.exports = function (connection) {
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    var locationSchema = new Schema({
        fb_location: String,
        coordinates:[]
    });

    var commentSchema = new Schema({
        data: String,
        image: [{ type : String }],
        commentor_id: { type: Schema.Types.ObjectId, ref: 'User_Fb' },
        created_at:{ type : Date, default: Date.now }
    });

    var feed_postSchema = new Schema({
        user_id:  { type: Schema.Types.ObjectId, ref: 'User_Fb' },
        content: String,
        location: [locationSchema],
        comment: [commentSchema],
        image: [{ type : String }],
        created_at: { type : Date, default: Date.now }
    });

    feed_postSchema.index({'location.coordinates':'2dsphere'});
    return connection.model('Feed_Post', feed_postSchema);
}

Server.js

app.get('/get_feed', function(req,res){
    if(req.param('distance') !== 'undefined'){
        Feed_Post.find(
        {
            'location.coordinates':
            {
                $nearSphere:
                {
                    $geometry:
                    {
                        type:'Point',
                        coordinates:[req.param('lng'),req.param('lat')]
                    }
                    ,$maxDistance:req.param('distance')
                }
            }
        }).populate('user_id','comment.commentor_id').sort({created_at:1}).exec(function(err,  result) {
            if (err) { return console.log(err); }
            res.json(result);
        });
    }else{
        res.json('error');
    }
});

答案示例Json:

   "_id" : ObjectId("562856e23ffdbb5f41510bee"),
   "user_id" :  {
                    ObjectId("5625d5d200ef06265deabfbe"),
                    'fb_id': 123456
                    'fb_image': "image_url"
                    'location': ""
                    'name': "Name Hello"
                }
   "content" : "Testing another post with comment",
   "created_at" : ISODate("2015-10-22T03:24:18.697Z"),
   "image" : [ ],
   "comment" : [
           {
                   "commentor_id" : ObjectId("5625d5d200ef06265deabfbe"),
                   "data" : "CraY comment",
                   "_id" : ObjectId("562856f23ffdbb5f41510bf0"),
                   "created_at" : ISODate("2015-10-22T03:24:34.546Z"),
                   "image" : [
                           "testimg_URL"
                   ]
           },
   ],
   "location" : [
           {
                   "fb_location" : "Earth",
                   "_id" : ObjectId("562856e23ffdbb5f41510bef"),
                   "coordinates" : [
                           100.2803235,
                           5.3314547
                   ]
           }
   ],
   "__v" : 0

注意:user_id 正在填充,但 commentor_id 未填充。

我找到了解决问题的方法。我填充的方式是错误的。我所做的是将两个填充数据放入 1 个填充语句

.populate('user_id','comment.commentor_id')

应该是这样写的

.populate('user_id').populate('comment.commentor_id')

只是一个解释

如前所述here:

In mongoose >= 3.6, we can pass a space delimited string of path names to populate. Before 3.6 you must execute the populate() method multiple times.

所以如果你这样写就可以了:

.populate('user_id' 'comment.commentor_id')

而不是你所说的逗号

这有效:填充('user_id comment.commentor_id')