Я пытаюсь обновить документ в MongoDB, используя mongoose. Я пробовал в updateOne() и updateMany(), но оба они дают одну и ту же ошибку. Помимо запросов Put req , Get, Post и Delete запросы работают нормально.
Код:
const uri =
"mongodb+srv://username:[email protected]/wikiDB?retryWrites=true&w=majority";
mongoose.connect(uri, { useNewUrlParser: true });
const articleSchema = new mongoose.Schema({
title: String,
content: String,
});
const Article = mongoose.model("Article", articleSchema);
app.put("/articles/:articleTitle", (req, res) => {
Article.updateOne(
{ title: req.params.articleTitle },
{ title: req.body.title, content: req.body.content },
{ overwrite: true }
).then((updated, e) => {
if (updated) {
res.send("Article successfully");
} else {
res.send(e);
}
});
});
Верхний код дает следующую ошибку:
/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:3787
throw new MongooseError('The MongoDB server disallows ' +
^
MongooseError: The MongoDB server disallows overwriting documents using `updateOne`. See: https://mongoosejs.com/docs/deprecations.html#update
at model.Query._updateThunk (/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:3787:13)
at model.Query._updateOne (/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:3881:23)
at model.Query.exec (/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:4330:28)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.16.0
[nodemon] app crashed - waiting for file changes before starting...
Я изменил код с Article.updateOne() на Article.updateMany() и Article.update(), но он дает следующие ошибки соответственно:
Ошибка №01:
/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:3787
throw new MongooseError('The MongoDB server disallows ' +
^
MongooseError: The MongoDB server disallows overwriting documents using `updateMany`. See: https://mongoosejs.com/docs/deprecations.html#update
at model.Query._updateThunk (/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:3787:13)
at model.Query._updateMany (/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:3868:23)
at model.Query.exec (/home/ahmedhassan/wiki-api/node_modules/mongoose/lib/query.js:4330:28)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v18.16.0
[nodemon] app crashed - waiting for file changes before starting...
Ошибка №02:
TypeError: Article.update is not a function
at /home/ahmedhassan/wiki-api/app.js:85:13
at Layer.handle [as handle_request] (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/route.js:144:13)
at next (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/route.js:140:7)
at next (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/route.js:140:7)
at Route.dispatch (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/layer.js:95:5)
at /home/ahmedhassan/wiki-api/node_modules/express/lib/router/index.js:284:15
at param (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/index.js:365:14)
at param (/home/ahmedhassan/wiki-api/node_modules/express/lib/router/index.js:376:14)

Вам больше не нужно перезаписывать этот код:
.put(async (req, res) => {
try {
await Article.updateOne(
{ title: req.params.articleTitle },
{
title: req.body.title,
content: req.body.content,
}
);
res.send("Article Updated");
} catch (err) {
console.info(err);
}});
Но если вы все еще хотите иметь что-то похожее на перезапись, вы можете использовать оператор $set:
.put(async (req, res) => {
try {
await Article.updateOne(
{ title: req.params.articleTitle },
{ $set: {
title: req.body.title,
content: req.body.content,
}}
);
res.send("Article Updated");
} catch (err) {
console.info(err);
}});
сообщение об ошибке дает ссылку mongoosejs.com/docs/deprecations.html#update попробовать с
replaceOne?