[英]Mongoose updating a document by ObjectId
我有一個問題,如果嘗試通過ObjectId更新文檔,則會收到MongooseError.CastError。 有什么幫助嗎?
var comment = new Comment({
contents: 'contents'
})
console.log(typeof req.body.postId) // 'string'
Post.update({_id: db.Types.ObjectId(req.body.postId)}, { // 'cast error'
commentsId: {$push: comment._id}
}, function(err, numAffected, res){
if (err) { return next(err)}
console.log('success')
})
讓Mongoose根據定義的架構為您進行轉換。
Post.update({_id: req.body.postId}, {commentsId: {$push: comment._id}}, ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.