node.js-创建后如何在猫鼬中填充子文档?

我要在item.comments列表中添加评论。 在响应中将其输出之前,我需要获取comment.created_by用户数据。 我应该怎么做?

    Item.findById(req.param('itemid'), function(err, item){
        var comment = item.comments.create({
            body: req.body.body
            , created_by: logged_in_user
        });

        item.comments.push(comment);

        item.save(function(err, item){
            res.json({
                status: 'success',
                message: "You have commented on this item",

//how do i populate comment.created_by here???

                comment: item.comments.id(comment._id)
            });
        }); //end item.save
    }); //end item.find

我需要在res.json输出中填充comment.created_by字段:

                comment: item.comments.id(comment._id)

comment.created_by是我的猫鼬CommentSchema中的用户参考。 它目前只给我一个用户ID,我需要它填充所有用户数据,密码和盐字段除外。

这是人们要求的架构:

var CommentSchema = new Schema({
    body          : { type: String, required: true }
  , created_by    : { type: Schema.ObjectId, ref: 'User', index: true }
  , created_at    : { type: Date }
  , updated_at    : { type: Date }
});

var ItemSchema = new Schema({
    name    : { type: String, required: true, trim: true }
  , created_by  : { type: Schema.ObjectId, ref: 'User', index: true }
  , comments  : [CommentSchema]
});
chovy asked 2019-11-08T06:23:54Z
4个解决方案
71 votes

为了填充引用的子文档,您需要显式定义ID引用到的文档集合(例如populate)。

如果已定义此引用,并且您的架构也已定义好,那么您现在就可以照常调用populate(例如populate('comments.created_by')

概念证明代码:

// Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
  name: String
});

var CommentSchema = new Schema({
  text: String,
  created_by: { type: Schema.Types.ObjectId, ref: 'User' }
});

var ItemSchema = new Schema({
   comments: [CommentSchema]
});

// Connect to DB and instantiate models    
var db = mongoose.connect('enter your database here');
var User = db.model('User', UserSchema);
var Comment = db.model('Comment', CommentSchema);
var Item = db.model('Item', ItemSchema);

// Find and populate
Item.find({}).populate('comments.created_by').exec(function(err, items) {
    console.log(items[0].comments[0].created_by.name);
});

最后请注意populate仅适用于查询,因此您需要先将项目传递到查询中,然后再调用它:

item.save(function(err, item) {
    Item.findOne(item).populate('comments.created_by').exec(function (err, item) {
        res.json({
            status: 'success',
            message: "You have commented on this item",
            comment: item.comments.id(comment._id)
        });
    });
});
jsalonen answered 2019-11-08T06:24:32Z
42 votes

自从编写原始答案以来,这种情况可能已经更改,但是看起来您现在可以使用Models填充函数来执行此操作,而不必执行额外的findOne。 请参阅:[http://mongoosejs.com/docs/api.html#model_Model.populate。]您想要在find处理程序中使用它,就像findOne一样。

user1417684 answered 2019-11-08T06:24:56Z
6 votes

@ user1417684和@ chris-foster是正确的!

工作代码摘录(无错误处理):

var SubItemModel = mongoose.model('subitems', SubItemSchema);
var ItemModel    = mongoose.model('items', ItemSchema);

var new_sub_item_model = new SubItemModel(new_sub_item_plain);
new_sub_item_model.save(function (error, new_sub_item) {

  var new_item = new ItemModel(new_item);
  new_item.subitem = new_sub_item._id;
  new_item.save(function (error, new_item) {
    // so this is a valid way to populate via the Model
    // as documented in comments above (here @stack overflow):
    ItemModel.populate(new_item, { path: 'subitem', model: 'subitems' }, function(error, new_item) {
      callback(new_item.toObject());
    });
    // or populate directly on the result object
    new_item.populate('subitem', function(error, new_item) {
      callback(new_item.toObject());
    });
  });

});
chhtm answered 2019-11-08T06:25:27Z
2 votes

我遇到了同样的问题,但是经过数小时的努力,我找到了解决方案。它可以不使用任何外部插件即可:)

applicantListToExport: function (query, callback) {
  this
   .find(query).select({'advtId': 0})
   .populate({
      path: 'influId',
      model: 'influencer',
      select: { '_id': 1,'user':1},
      populate: {
        path: 'userid',
        model: 'User'
      }
   })
 .populate('campaignId',{'campaignTitle':1})
 .exec(callback);
}
Naveen Kumar answered 2019-11-08T06:25:51Z
translate from https://stackoverflow.com:/questions/13026486/how-to-populate-a-sub-document-in-mongoose-after-creating-it