-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoUtils.js
41 lines (33 loc) · 1.1 KB
/
mongoUtils.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
40
41
const { MongoClient } = require('mongodb');
// MongoDB Configuration
const MONGO_URL = 'mongodb://localhost:27017';
const DB_NAME = 'academic_sources';
const client = new MongoClient(MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true });
const db = client.db(DB_NAME);
db.collection('articles_collection').dropIndex("uniqueID_1");
module.exports = {
/**
* Connect to MongoDB.
* @returns {Promise} Resolves with the database instance.
*/
connect: async function() {
await client.connect();
return client.db(DB_NAME);
},
/**
* Close the MongoDB connection.
*/
close: function() {
return client.close();
},
/**
* Insert documents into a specified collection.
* @param {Array} docs - The documents to insert.
* @param {string} collectionName - The name of the collection.
* @returns {Promise} Resolves with the result of the insertion.
*/
insertMany: async function(docs, collectionName) {
const db = client.db(DB_NAME);
return await db.collection(collectionName).insertMany(docs);
}
};