Mongoose 只推送 ObjectID,而不是整个文档

Mongoose pushing only ObjectID, not the whole document

我有一个菜谱博客网站,每个菜谱都有评论。每个评论都保存到评论集合中,但是当我将评论推送到正确的配方时,而不是只保存 ObjectID。这是我的代码:

配方模型

const { Double } = require('bson');
const mongoose = require('mongoose');

const recipeSchema = new mongoose.Schema({
    name: {
        type: String,
        required: 'This field is required.'
    },
    description: {
        type: String,
        required: 'This field is required.'
    },
    quantity: {
        type: Array,
        required: 'This field is required.'
    },
    ingredients: {
        type: Array,
        required: 'This field is required.'
    },
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Comment'
        }
    ],
    recipe_id: {
        type: String,
    }

});

recipeSchema.index({ name: 'text', description: 'text' });
const Recipe = module.exports = mongoose.model('Recipe', recipeSchema);

评论模型

const commentSchema = new mongoose.Schema({
    username: {
        type: String,
        required: 'This field is required.'
    },
    comment: {
        type: String,
        required: 'This field is required.'
    },
    recipe_id: {
        type: String
    }
});

路线

router.get('/', recipeController.homepage);
router.get('/recipe/:id', recipeController.exploreRecipe );
router.get('/categories/:id', recipeController.exploreCategoriesById);
router.get('/categories', recipeController.exploreCategories);
router.get('/submit-recipe', recipeController.submitRecipe);
router.post('/submit-recipe', recipeController.submitRecipeOnPost);
router.post('/recipe/:id/comments', recipeController.CommentRecipeOnPost);

控制器

module.exports.CommentRecipeOnPost = async(req, res) => {
    const comment = new Comment({
      username: req.body.username,
      comment: req.body.comment
    });
    comment.save((err, result) => {
      if (err){
        console.log(err)
      }else {
        Recipe.findById(req.params.id, (err, post) =>{
          if(err){
            console.log(err);
          }else{
            post.comments.push(result);
            post.save();

            console.log('====comments=====')
            console.log(post.comments);
            res.redirect('/');
          }
        })
      }
    })
   }

我尝试使用填充和其他方法,但没有人工作,经过大量小时的编程后,我用这些方法完成了,我只能保存 ObjectId-s。

这是因为您将 commentsrecipe schema 定义为 ObjectId,这是正确的,如果您想获得完整的评论参数,您将填充记录

const recipe = await Recipe.findOne({ _id: "recordId"}).populate("posts") 

这将 return 所有评论详情

{  name: "Fitness recipe", description: "Fitness recipe to loss 10kg in 10 days only", comments: [ {_id: "recordId", username: "Smith June", comment: "That's awesome"}]}

如果您需要将整个文档保存为一个对象数组,而不仅仅是 id,那么您可以创建这样的模式

const recipeSchema = new mongoose.Schema({
    name: {
        type: String,
        required: 'This field is required.'
    },
    description: {
        type: String,
        required: 'This field is required.'
    },
    quantity: {
        type: Array,
        required: 'This field is required.'
    },
    ingredients: {
        type: Array,
        required: 'This field is required.'
    },
    comments: [
        { username: String, comment: String }
    ],
    recipe_id: {
        type: String,
    }

})

而且在这种情况下,您不会有任何关系,您可以轻松列出食谱评论。