我正在为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
Run Code Online (Sandbox Code Playgroud)
我需要在res.json输出中填充comment.created_by字段:
comment: item.comments.id(comment._id)
Run Code Online (Sandbox Code Playgroud)
comment.created_by是我的mongoose CommentSchema中的用户引用.它目前只给我一个用户ID,我需要填充所有用户数据,密码和盐字段除外.
以下是人们提出的架构:
var CommentSchema = new Schema({
body : { type: String, required: true }
, created_by : { type: Schema.ObjectId, ref: 'User', index: true }
, created_at : { type: Date }
, …Run Code Online (Sandbox Code Playgroud) 我来自一个sql背景,所以在我连接表的sql中编写查询非常简单,但我想我在mongoose/mongodb中遗漏了
基本上我知道Subscriber_ID(映射到User Collection中的文档)
我想拉动项目组,包含用户所属的所有项目,所以如果我在pseduo sql中写这个,那就像
Select
ProjectGroup.title,
Project.Title
FROM
ProjectGroup,
Project,
User
WHERE
User.id = req.body.subscriber_id
AND Project.subscriber_id = User.id
AND ProjectGroup.project_id = Project.id
Run Code Online (Sandbox Code Playgroud)
必须有一种方法可以在mongoose/mongodb中进行类似的连接,因为类型正在映射到模式吗?
我的架构.....
项目组架构
var ProjectGroupSchema = new Schema({
title : String
, projects : [ { type: Schema.Types.ObjectId, ref: 'Project' } ]
});
Run Code Online (Sandbox Code Playgroud)
项目架构
var ProjectSchema = new Schema({
title : {type : String, default : '', required : true}
, subscribers : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
Run Code Online (Sandbox Code Playgroud)
用户架构
var UserSchema = new Schema({ …Run Code Online (Sandbox Code Playgroud)