[英]How to increase values in Mongo DB
I want to increase the current number in the database.我想增加数据库中的当前数字。
Example: If the number in the database is 0 I want it to increase by 1 every time I use the PostImage command.示例:如果数据库中的数字为 0,我希望它在每次使用 PostImage 命令时增加 1。
The Error: When I console.log(debug) It gives undefined错误:当我 console.log(debug) 它给出 undefined
const attachment = await interaction.options.getAttachment("input");
const caption = await interaction.options.getString("caption");
const channel = client.channels.cache.get("988214204660064286");
const embed = new MessageEmbed()
.setAuthor({ name: `${username} Posted`, iconURL: `${profilePicData}` })
.setColor("#303236")
.setDescription(`${caption}`)
.setImage(`${attachment.url}`)
.setFooter({ text: `user ID: ${userId}`, iconURL: `${profilePicData}` })
channel.send({ embeds: [embed] }).then(async (msg) => {
await msg.react("👎");
await msg.react("👍");
});
db.findOneAndUpdate({
UserId: userId,
PostAmount: 1
})
const dubug = data.map((x) => `${x.PostAmount}`).join('\n');
console.log(dubug)
Increasing a data on mongoose
uses {$inc: {your_data}}
在mongoose
上增加数据使用{$inc: {your_data}}
db.findOneAndUpdate({
UserId: userId,
PostAmount: 1 //You can now remove these one.
}, {$inc: {PostAmount: +1}}, async(err, data) => {
if(data) {
data.PostAmount += 1;
await data.save();
}
})
You should remove these code line PostAmount: 1 because
UserId
Will find its own data on database.您应该删除这些代码行PostAmount: 1因为UserId
会在数据库中找到它自己的数据。
To increase the value of PostAmount
by 1, you can simply get the document, change the value of PostAmount
to PostAmount + 1
and save the document要将PostAmount
的值增加 1,您可以简单地获取文档,将 PostAmount 的值更改为PostAmount
PostAmount + 1
并保存文档
let doc = await db.findOne({ UserId: userId });
doc.PostAmount = doc.PostAmount + 1;
await doc.save();
Besides that, debug
returns undefined because it is not defined.除此之外, debug
返回 undefined 因为它没有定义。 It is probably because in the debug definition it tries to map data
which I don't see defined either.这可能是因为在调试定义中它试图映射我也没有看到定义的data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.