创建后如何在 mongoose 中填充子文档? [英] How to populate a sub-document in mongoose after creating it?
问题描述
我正在向 item.comments 列表添加评论.在响应中输出之前,我需要获取 comment.created_by 用户数据.我该怎么做?
I am adding a comment to an item.comments list. I need to get the comment.created_by user data before I output it in the response. How should I do this?
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 字段:
I need to populate the comment.created_by field here in my res.json output:
comment: item.comments.id(comment._id)
comment.created_by 是我的猫鼬 CommentSchema 中的用户引用.它目前只给我一个用户 ID,我需要用所有用户数据填充它,密码和盐字段除外.
comment.created_by is a user reference in my mongoose CommentSchema. It currently is only giving me a user id, I need it populated with all the user data, except for password and salt fields.
这是人们询问的架构:
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]
});
推荐答案
为了填充引用的子文档,您需要明确定义 ID 引用的文档集合(例如 created_by: { type: Schema.Types.ObjectId, ref: 'User' }
).
In order to populate referenced subdocuments, you need to explicitly define the document collection to which the ID references to (like created_by: { type: Schema.Types.ObjectId, ref: 'User' }
).
鉴于此引用已定义并且您的架构也已定义良好,您现在可以像往常一样调用 populate
(例如 populate('comments.created_by')
)
Given this reference is defined and your schema is otherwise well defined as well, you can now just call populate
as usual (e.g. 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
仅适用于查询,因此您需要先将您的项目传递到查询中,然后再调用它:
Finally note that populate
works only for queries so you need to first pass your item into a query and then call it:
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)
});
});
});
这篇关于创建后如何在 mongoose 中填充子文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!