【问题标题】:How to populate a sub-document in mongoose after creating it?创建后如何在 mongoose 中填充子文档?
【发布时间】:2012-10-13 03:38:06
【问题描述】:

我正在向 item.cmets 列表添加评论。在将其输出到响应中之前,我需要获取 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]
});

【问题讨论】:

  • 你能详细说明一下架构吗
  • 我已经粘贴了我尝试过的,res.json()

标签: node.js mongodb express mongoose


【解决方案1】:

为了填充引用的子文档,您需要显式定义 ID 引用的文档集合(如 created_by: { type: Schema.Types.ObjectId, ref: 'User' })。

鉴于已定义此引用并且您的架构也已明确定义,您现在可以像往常一样调用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)
        });
    });
});

【讨论】:

  • 我重新阅读了你的 item.save() 函数,这主要是有效的,除了我把集合中的所有 cmets 都找回来了,我只想要我添加的那个。
  • 好的,我遇到了一个小错误,populate('cmets.created_by username') 不起作用,但 .populate('cmets.created_by', 'username') 起作用了。我还注意到我可以使用 -salt -password_hash 不返回这些字段。非常感谢!接受的答案。
  • 哇哦!我不知道你可以使用- 前缀。也谢谢你教我! :)
  • @chovy 你能用你的-salt -passowrd_hash 方法更新代码吗?我搜索了 mongoose 文档,但没有提到 -salt。
  • 2014 年更新:在下方查看 user1417684 的答案。 stackoverflow.com/a/17505554/825980
【解决方案2】:

自从编写了原始答案以来,这可能已经改变,但看起来您现在可以使用模型填充函数来执行此操作,而无需执行额外的 findOne。请参阅:http://mongoosejs.com/docs/api.html#model_Model.populate。您会想在保存处理程序中使用它,就像 findOne 一样。

【讨论】:

  • 这个答案比@jsalonen 的答案正确得多。这样您就不必重新获取文档。
  • 参考资料很有帮助,谢谢。一个例子更进一步。
【解决方案3】:

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

});

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-12-04
      • 2018-08-16
      • 2017-01-27
      • 1970-01-01
      • 2014-08-16
      • 2020-12-18
      • 2015-08-08
      • 2017-03-21
      • 2017-06-06
      相关资源
      最近更新 更多