如何在MongoDB中的数组中添加对象?

时间:2018-11-26 00:19:19

标签: mongodb

我有这个模式:

var PostSchema = new Schema({
  title: String,
  description: String,
  content: String,
  image: String,
  category: String,
  url: String,
  views: {
    type: Number,
    default: 0
  },
  comments: [
    {
      username: String,
      email: String,
      comment: String
    }
  ]
});

现在,我需要在注释数组中放置新对象。我已经做了这种方法,但是显然不起作用:)

router.put('/blog/:url/add-comment', (req, res) => {
    let db = req.db
    Post.updateOne({ 'url': req.params.url }, {$push: {comments: {
        'username': req.body.comments.username,
        'email': req.body.comments.email,
        'comment': req.body.comments.comment
    }}})

我哪里出错了?

0 个答案:

没有答案