我正在使用updateOne()更新一个属性的值,但是会影响名称相似的其他属性。 (使用猫鼬5.9.9)
var file = new Schema({
name: String,
file_image: String,
file_image_preview: String,
});
// I have this document
{
name: 'file.jpg',
file_image: 'http://localhost/image371.jpg',
file_image_preview: 'http://localhost/image371_preview.jpg',
}
// I use the updateOne method
file.updateOne({_id: '5e969e2e43c2433470c19f67'}: ,
{ $set: {
file_image_preview: 'http://localhost/image371updated_preview.jpg'
}
})
// The document is updated to this
{
name: 'file.jpg',
file_image: 'http://localhost/image371updated_preview.jpg',
file_image_preview: 'http://localhost/image371updated_preview.jpg',
}
命名约定有问题吗?
答案 0 :(得分:1)
尝试使用findOneAndUpdate()方法
const filter = { _id: '5e969e2e43c2433470c19f67' };
const update = {
file_image_preview: 'http://localhost/image371updated_preview.jpg'
};
file.findOneAndUpdate(filter, update);