我试图在Mongoose中使用updateMany将值重置为默认值。但是,它一直在获取500 Internal Server Error
。我想念哪里?
Index.js
app.put('/reset-match', (req, res) => {
Activity.updateMany({}, { $set: { color: "#ffffff" }},{upsert: true}, {multi: true}, (err,doc) => {
if(err) {res.status(400).send(err)}
else res.status(205).send(doc)
});
});
活动模型
const mongoose = require('mongoose');
const validator = require('validator');
const Activity = mongoose.model('activity', {
name: {
type: String,
required: true,
trim: true
},
icon: {
type: String,
required: true,
trim: true,
validate(value) {
if (!validator.isURL(value)) {
throw new Error('URL is invalid');
}
}
},
color: {
type: String,
default: '#fff'
}
})
module.exports = Activity;
```
I expect the syntax of UpdateMany which works!
答案 0 :(得分:1)
您正试图使用太多参数调用updateMany()
函数
Activity.updateMany({}, { $set: { color: "#ffffff" }},{upsert: true}, {multi: true}, (err,doc) => { ... })基于mongoose docs,所有选项都应由一个对象传递,如下所示:
Activity.updateMany({}, { $set: { color: "#ffffff" }},{ upsert: true, limit: 20, skip: 50 }, (err,doc) => { ... })
此外,借助updateMany()
功能,您无需使用multi: true
选项。仅在使用update()
函数时才有意义。
尝试以下代码:
Activity.updateMany({}, { $set: { color: "#ffffff" }}, { upsert: true }, (err, data) => { if (err) { res.status(400).send(err) } res.send(data) })
答案 1 :(得分:1)
这是我们的处理方式:
askUser();
function askUser(){
let answer = prompt('Hello would you please enter a number between 4 and 10?');
if( answer>4 && answer<10 )
alert('Thank you, you are awesome!')
else if( answer === null )
alert('Really you can\'t think of a number between 4 and 10??!?!?')
else
askUserAgain();
}
function askUserAgain() {
let answer = prompt('This is not a number between 4 and 10, you are a fool! Please try again.');
if( answer>4 && answer<10 )
alert('Thank you, you are awesome!')
else if( answer === null )
alert('Really you can\'t think of a number between 4 and 10??!?!?')
else
askUserAgain();
}
所以这是一个基于文档的示例:
db.collection.updateMany(condition, update, options, callback function)
这对我来说很好,希望对您有帮助:)