aboutsummaryrefslogtreecommitdiff
path: root/db/mongodb.js
diff options
context:
space:
mode:
authorjourney-ad <[email protected]>2023-03-06 18:57:41 +0800
committerJad <[email protected]>2023-03-06 18:59:42 +0800
commitc3f18f8b34bd8403a040a263bc8354bc7a9b4eae (patch)
tree411352810cfd757b276878eb544d6ebd0e4e6202 /db/mongodb.js
parentUpdate Readme.md (diff)
downloadcounter-c3f18f8b34bd8403a040a263bc8354bc7a9b4eae.tar.xz
counter-c3f18f8b34bd8403a040a263bc8354bc7a9b4eae.zip
perf: Delayed writing to database
- Implement delayed writing feature to database for improved performance and reduced write frequency - Update docs
Diffstat (limited to 'db/mongodb.js')
-rw-r--r--db/mongodb.js18
1 files changed, 17 insertions, 1 deletions
diff --git a/db/mongodb.js b/db/mongodb.js
index 57f1b72..4119c7a 100644
--- a/db/mongodb.js
+++ b/db/mongodb.js
@@ -32,8 +32,24 @@ function setNum(name, num) {
.exec()
}
+function setNumMulti(counters) {
+ const bulkOps = counters.map(obj => {
+ const { name, num } = obj
+ return {
+ updateOne: {
+ filter: { name },
+ update: { name, num },
+ upsert: true
+ }
+ }
+ })
+
+ return Count.bulkWrite(bulkOps, { ordered : false })
+}
+
module.exports = {
getNum,
getAll,
- setNum
+ setNum,
+ setNumMulti
}