-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
39 lines (33 loc) · 1.5 KB
/
mongo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const userName = process.env.MONGO_DB_USERNAME;
const password = process.env.MONGO_DB_PASSWORD;
const { MongoClient, ServerApiVersion } = require('mongodb');
async function createClient(userName,password) {
const uri = `mongodb+srv://${userName}:${password}@cluster0.x0izjpz.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
//await client.connect();
return client;
}
async function searchProduct(client,databaseAndCollection,styleID){
let filter = {styleID: styleID};
const result = await client.db(databaseAndCollection.db)
.collection(databaseAndCollection.collection)
.findOne(filter);
if (result) {
return result;
}
return false;
}
async function insertProduct(client, databaseAndCollection,product) {
const result = await client.db(databaseAndCollection.db).collection(databaseAndCollection.collection).insertOne(product);
}
async function deleteProduct(client, databaseAndCollection, styleID){
let filter = {styleID: styleID};
const result = await client.db(databaseAndCollection.db)
.collection(databaseAndCollection.collection)
.deleteOne(filter);
console.log(`Documents deleted ${result.deletedCount}`);
}
exports.createClient = createClient;
exports.searchProduct = searchProduct;
exports.insertProduct = insertProduct;
exports.deleteProduct = deleteProduct;