简体   繁体   中英

How to update a value inside an object with schema.findoneandupdate (Mongodb/mongoose)

I'm making a discord bot in discord.js and I want to make a database that keeps track of user messages, I've got the tracking part and the database structure down, which goes as follows:

_id: guildId,
metrics: {
  [userId]: 
  {
    messageCount: 1
  }
}

I'm using a mongoose schema, so how would I use schema.findoneandupdate to incrementally increase the messageCount value?

You can use $inc operator:

More detail: https://docs.mongodb.com/manual/reference/operator/update/inc/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM