我有这个猫鼬模式
var mongoose = require('mongoose');
var ContactSchema = module.exports = new mongoose.Schema({
name: {
type: String,
required: true
},
phone: {
type: Number,
required: true,
index: {unique: true}
},
messages: [
{
title: {type: String, required: true},
msg: {type: String, required: true}
}]
}, {
collection: 'contacts',
safe: true
});
并尝试通过这样做来更新模型:
Contact.findById(id, function(err, info) {
if (err) return res.send("contact create error: " + err);
// add the message to the contacts messages
Contact.update({_id: info._id}, {$push: {"messages": {title: title, msg: msg}}}, function(err, numAffected, rawResponse) {
if (err) return res.send("contact addMsg error: " + err);
console.log('The number of updated documents was %d', numAffected);
console.log('The raw response from Mongo was ', rawResponse);
});
});
我不是在声明messages
要获取一组对象吗?
错误: MongoError:无法将 $push/$pushAll 修饰符应用于非数组
有任何想法吗?