【发布时间】:2021-07-10 16:49:14
【问题描述】:
# I can't update keep getting { ok: 0, n: 0, nModified: 0 } ! # ... const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mongo-exercises', {useNewUrlParser: true, useUnifiedTopology: true }) ... .then(() => console.log('连接到 MongoDB...')) .catch(() => console.error('Could not connect to MongoDB...', err));
...
const courseSchema = new mongoose.Schema({
tags: [String],
date: {type: Date, default: Date.now},
name: String,
isPublished: Boolean,
author: String,
Price: Number
...
});
...
const Course = mongoose.model('Course', courseSchema);
async function updateCourse(id){
const result = await Course.updateOne({_id: id}, {
$set: {
author: 'Momo',
isPublished: true
}
});
...
console.log(result); //{ n: 0, nModified: 0, ok: 1 }
}
...
updateCourse('5a68ff090c553064a218a547');
块引用
【问题讨论】:
-
如果您使用 Postmon 发送请求,请确保在
Body中选择JSON。默认的Text不起作用,会报这个错误。