追加到猫鼬填充数组?

时间:2019-03-15 20:36:57

标签: javascript node.js database mongodb mongoose

我有以下猫鼬模型:

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

var CommentSchema = new Schema({
  body: {type: String, required: true, max: 2000},
  created: { type: Date, default: Date.now },
  flags: {type: Number, default: 0},
  fileName: {type: String, default: ""}
}, {
  writeConcern: {
    w: 0,
    j: false,
    wtimeout: 200
  }  
});

var PostSchema = new Schema({
  body: {type: String, required: true, max: 2000},
  created: { type: Date, default: Date.now },
  flags: {type: Number, default: 0},
  fileName: {type: String, default: ""},
  comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
}, {
  writeConcern: {
    w: 0,
    j: false,
    wtimeout: 200
  }  
});

var Post =  mongoose.model('Post', PostSchema);
var Comment = mongoose.model('Comment', CommentSchema)

module.exports = {
  Post: Post,
  Comment: Comment
} 

我希望能够使用populate方法在发布后的评论数组中添加评论,但是我已经浏览了很多 在线示例,但我还没有找到一种简单的方法。有人有建议吗?

0 个答案:

没有答案