【问题标题】:MongoDB: Multiple populate subdocuments not being returnedMongoDB:未返回多个填充子文档
【发布时间】:2016-01-21 07:09:46
【问题描述】:

我正在尝试返回文档和子文档的完整数据对象。它返回的是文档和子文档的 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);;
}

Feed 架构

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);
}

服务器.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。

【问题讨论】:

    标签: node.js mongodb model mongoose schema


    【解决方案1】:

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

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

    它应该是这样写的。

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

    【讨论】:

      【解决方案2】:

      只是解释

      here所说:

      在 mongoose >= 3.6 中,我们可以传递一个以空格分隔的路径名字符串 填充。 3.6之前必须执行populate()方法 多次。

      所以如果你这样说它会起作用:

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

      而不是像你所说的那样用逗号

      【讨论】:

        【解决方案3】:

        这有效:populate('user_id comment.commentor_id')

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-06
          • 2014-11-20
          • 2020-12-05
          • 2018-01-26
          • 2021-06-03
          • 2017-04-02
          • 2020-11-24
          • 2021-06-17
          相关资源
          最近更新 更多