如何用猫鼬填充作为mongodb文档子文档的文档

时间:2019-07-28 23:57:10

标签: node.js mongodb express mongoose

我在node js,express,mongo db中创建了一个博客,我想建立一个带有答案的评论系统。在我看来,答案就是评论。在我的评论模型中,我有一个答案表:[this]。在我的文章moddel中,我有一个注释表。

我的问题是如何填充答案表中的用户

这是我的评论模型或架构:

const mongoose = require("mongoose");
const CommentSchema = mongoose.Schema({
  comment: {
    type: String,
    minlength: 10,
    maxlength: 1000,
    required: true
  },
  code: {
    type: String
  },
  image: {
    type: String,
    minlength: 10,
    maxlength: 80
  },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true
  },
  responses: [this] // but response is a comment
});

const Comment = mongoose.model("Comment", CommentSchema);
module.exports = {
  Comment,
  CommentSchema
};

这是我的文章模型:

const mongoose = require("mongoose");
const Comment = require("../Models/Comment");
const ArticleSchema = mongoose.Schema({
  title: {
    type: String,
    minlength: 6,
    maxlength: 255,
    required: true
  },
  slug: {
    type: String,
    minlength: 4,
    maxlength: 255,
    required: true,
    unique: true
  },
  content: {
    type: String,
    required: true
  },
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Admin",
    required: true
  },
  categorie: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Categorie",
    required: true
  },
  isPublish: {
    type: Boolean,
    default: false
  },
  created_at: {
    type: Date,
    default: Date.now
  },
  comments: [Comment.CommentSchema]
});

const Article = mongoose.model("Article", ArticleSchema);

module.exports = Article;

这是我的用户模型

 const mongoose = require("mongoose");

    const UserSChema = mongoose.Schema({
          name: {
            type: String,
            minlength: 2,
            maxlength: 80,
            required: true,
            match: /^[a-zA-Z]/
          },
          firstname: {
            type: String,
            minlength: 4,
            maxlength: 150,
            required: true,
            match: /^[a-zA-Z]/
          },
          email: {
            type: String,
            minlength: 6,
            maxlength: 50,
            required: true,
            unique: true
          },
          avatar: {
            type: String,
            maxlength: 80,
            required: true,
            default: "default.png"
          },
          password: {
            type: String,
            minlength: 6,
            maxlength: 255,
            required: true
          },
          token_comfirm: {
            type: String,
            maxlength: 255,
            required: true,
            default: null
          }
        });

        const User = mongoose.model("User", UserSChema);

        module.exports = User;

我到达时是通过

填充评论数组中的用户的
 Article.findById("5d3d6e2c11487f645874abe1")
      .populate("comments.user")
      .then(article => {

        // Show article detail
        console.log(article);

        // get details of each comment
        article.comments.forEach(c => {
          console.log(c);
        });
      });

但是我无法填充comment.responses数组中的用户。我尝试了这个,但是它不起作用:

 Article.findById("5d3d6e2c11487f645874abe1")
      .populate("comments.responses.user")
      .then(article => {

        // Show article detail
        console.log(article);

        // get details of each comment
        article.comments.forEach(c => {
          console.log(c);
        });
      });

0 个答案:

没有答案