Embezzle/mongoHelp.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-02-15 10:01:01 +00:00
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017'
2020-02-15 23:22:09 +00:00
var options= {
useNewUrlParser: true,
useUnifiedTopology: true,
promiseLibrary: global.Promise}
2020-02-15 10:01:01 +00:00
function put(db,collection, object){
2020-02-15 23:22:09 +00:00
const client = new MongoClient(url, options)
2020-02-15 10:01:01 +00:00
client.connect()
client.db(db).collection(collection).insertOne(object).then((value) =>{client.close();return value})
}
exports.put = put
function get(db, collection, object){
2020-02-15 23:22:09 +00:00
const client = new MongoClient(url, options)
2020-02-15 10:01:01 +00:00
client.connect()
return client.db(db).collection(collection).find(object).toArray().then((value) =>{client.close();return value})
}
exports.get = get
function getUnique(db, collection, field,object){
2020-02-15 23:22:09 +00:00
const client = new MongoClient(url, options)
2020-02-15 10:01:01 +00:00
client.connect()
return client.db(db).collection(collection).distinct(field,object).then((value) =>{client.close(); console.log(value);return value})
}
exports.getUnique = getUnique
function deleteOne(db, collection, object) {
2020-02-15 23:22:09 +00:00
const client = new MongoClient(url, options)
2020-02-15 10:01:01 +00:00
client.connect()
client.db(db).collection(collection).deleteOne(object).then((value) =>{client.close();return value})
}
2020-02-15 14:59:27 +00:00
exports.deleteOne = deleteOne
function update(db, collection, search,update) {
2020-02-15 23:22:09 +00:00
const client = new MongoClient(url, options)
2020-02-15 14:59:27 +00:00
client.connect()
2020-02-15 23:22:09 +00:00
client.db(db).collection(collection).updateOne(search,{$set:update}).then((value) =>{client.close();return value})
2020-02-15 14:59:27 +00:00
}
exports.update = update