无法将对象推入Mongodb集合中的数组,但插入了ID

时间:2018-09-28 10:30:13

标签: node.js mongodb express mongoose

我在架构中使用注释数组作为休假。我想使用nodejs api

将评论数据推入该评论数组中
    var Schema = mongoose.Schema;


 var myfeeds = new Schema({
title: {
    type: String,
    required: true
},
feed: {
    type: String,
    required: true
},
createdBy: {
    type: String,
    required: true,
    unique: true
},
createdDate: {
    type: Date,
    required: true,
    default: Date.now()
},

comment: [
    {
        commentBy: {
            type: String
        },
        commentText: {
            type: String
        },
        createdDate: {
            type: Date
        }
    }
],
likes: [
    {
        likesCount: {
            type: Number,
            required: false
        },
        likeBy: {
            type: String,
            required: false
        }
    }
]
  });

我想将对象推送到此注释数组。所以,为此,我要告诉我这样做是否有误

let _id = req.body.id;
let commentBy = req.body.commentedBy;
let commentedText = req.body.commentedText;
let commentedDate = req.body.commentedDate;
let data = {
    commentBy: commentBy,
    commentText: commentedText,
    createdDate: commentedDate
};

MyFeeds.findByIdAndUpdate(
    { _id: _id },
    {
        $push: {
            comment: data
        }
    }
)
    .then((result) => {
        res.status(200).json({
            status: result
        });
    })
    .catch((err) => {
        res.status(500).json({
            status: 'invalid',
            err: err
        });
    });

但仅将id插入该评论数组,而不插入必需的内容

    "comment": [
                   {
            "_id": "5badfd092b73fa14f4f0aa7c"
        },
        {
            "_id": "5badfd102b73fa14f4f0aa7d"
        },
        {
            "_id": "5badfd142b73fa14f4f0aa7e"
        },
        {
            "_id": "5badfd31500fb11bb06b4c8a"
        },
        {
            "_id": "5badfd35500fb11bb06b4c8b"
        },
        {
            "_id": "5badff3d439a151190d62961"
        }
    ],

0 个答案:

没有答案