Node.js 如何使用mongoose更新数组中的现有对象

Node.js 如何使用mongoose更新数组中的现有对象,node.js,mongoose,discord.js,Node.js,Mongoose,Discord.js,我正试图用我的discord.js机器人和猫鼬创建一个货币系统。以下是MongoDB文档格式示例: { 公会:“2095843098435435”, 钱包:[ { id:“232335354”, 金额:10, }, { id:“24344343234454”, 金额:“我想更新这个”, }, ], }; 据我所知,该函数用于在数组中创建新对象 但是如何更新数组中的现有对象 我的代码: const find=wait Account.findOne({ 帮会:message.guild.id,

我正试图用我的discord.js机器人和猫鼬创建一个货币系统。以下是MongoDB文档格式示例:

{
公会:“2095843098435435”,
钱包:[
{
id:“232335354”,
金额:10,
},
{
id:“24344343234454”,
金额:“我想更新这个”,
},
],
};
据我所知,该函数用于在数组中创建新对象

但是如何更新数组中的现有对象

我的代码:

const find=wait Account.findOne({
帮会:message.guild.id,
});
if(!find)返回message.channel.send(“…”);
const memberRole=message.guild.members.cache
.find((x)=>x.id==message.author.id)
.roles.cache.find(
(x) =>
x、 name.toLowerCase()=“锦标赛管理器”||
x、 name.toLowerCase()=“事件人员”||
x、 name.toLowerCase()=“钱包管理器”
);
if(message.member.hasPermission(“管理员”)| | memberRole){
if(!args[2])返回消息。回复(“…”);
const-title=message.indications.users.first();
如果(!提及)返回消息。回复(“…”);
const account=find.wallets.find((x)=>x.id==提及.id);
如果(!account)返回message.reply(“…”);
if(!args[3])返回message.reply(“…”);
if(isNaN(args[3])返回message.channel.send(“…”);
const update=account.update({
金额:(account.amount+=args[3]),
});
等待查找。保存();
}

您可以使用
$set
操作符更新您的收藏,类似这样的操作会有所帮助

db.collection.update({"wallets.id": "24344343234454"},{$set: {
        "wallets.$.amount": "This is new_value"
    }}
你可以阅读更多关于


$
符号是位置运算符,用于保存在第一个表达式中匹配的数组项的位置(索引)。更多信息。

“这是新值”我的当前值设置为
account.amount+=args[3]
,但我现在不知道该用account替换什么。应该是
wallets.amount
还是
find.wallets.amount
#等等,伙计,我用的是mongoose,mongoose npm包,不是mongodb