Moe-Counter/db/mongodb.js

40 lines
749 B
JavaScript
Raw Normal View History

'use strict'
const mongoose = require('mongoose')
const schema = require('./schema')
2020-08-21 16:11:20 +00:00
// the default mongodb url (local server)
2020-08-22 12:36:34 +00:00
const mongodbURL = process.env.DB_URL || 'mongodb://127.0.0.1:27017'
2020-08-21 16:11:20 +00:00
mongoose.connect(mongodbURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
const Count = mongoose.connection.model('Count', schema)
function getNum(name) {
return Count
.findOne({ name }, '-_id -__v')
.exec()
}
function getAll() {
return Count
.find({ }, '-_id -__v')
.exec()
}
function setNum(name, num) {
return Count
.findOneAndUpdate({ name }, { name, num }, { upsert: true })
.exec()
}
module.exports = {
getNum,
getAll,
setNum
2020-08-22 12:36:34 +00:00
}