如何使用findByIdAndUpdate将objectId推送到对象的mongodb模式字段数组

每当我打我的API我得到同样的错误 – 我曾尝试发送值作为参数也失败了。 任何帮助,将不胜感激。 当我使用$set每次调用Web服务时更新相同的值,但它确实工作,但不是与$push

MongoError:字段'songId'必须是一个数组,但在文档中是objectIdtypes的

  {_id: ObjectId('59709590380026118c22dd61')} 

我的播放列表模式代码: –

 var PlaylistSchema = new mongoose.Schema({ name: String, coverphoto: { type: String, default: '' }, userId: { type: ObjectId, ref: 'User' }, songId: [{ type: ObjectId, ref: 'Song' }], updated_at: { type: Date, default: Date.now }, }); 

我的宋架构代码

 var mongoose = require('mongoose'); var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var SongSchema = new mongoose.Schema({ audioName:{type:String,default:''}, title: String, // coverphoto: { type: String, default: '' }, singer: { type: String, default: '' }, movie: { type: String, default: '' }, album: { type: String, default: '' }, lyricist: { type: String, default: '' }, actors: { type: String, default: '' }, lyrics: { type: String, default: '' }, genre: { type: String, default: 'Random' }, duration: { type: String, default: '' }, size: { type: String, default: '' }, userId: { type: ObjectId, ref: 'User' }, categoryId: { type: ObjectId, ref: 'Category' }, updated_at: { type: Date, default: Date.now }, }); module.exports = mongoose.model('Song', SongSchema); 

我的Api代码

 /* post api to add song in playlist */ router.post('/addSongToPlaylist', function (req, res, next) { Playlist.findOne({ '_id': req.body.playlistId }, function (err, playlist) { if (err) return next(err); console.log(playlist) console.log("req.body.songId", req.body.songId); if (playlist) { Playlist.findByIdAndUpdate( req.body.playlistId, { $push: { "songId": req.body.songId } }, { new: true }, function (err, playlistData) { if (err) return next(err); res.json({ message: 'New Song added successfully', playlist: playlistData, success: true }); }); } else if (!Song) { res.json({ message: 'Failed to add song', success: false }); } }); });