webfast 0.1.82 → 0.1.83
Sign up to get free protection for your applications and to get access to all the features.
- package/modules/data/mongo/aggregate.js +48 -48
- package/package.json +1 -1
@@ -1,52 +1,52 @@
|
|
1
1
|
const { MongoClient } = require('mongodb');
|
2
|
-
module.exports = async function(program,db,collection,query,callback) {
|
3
|
-
// Ensure the MongoDB connection string is provided
|
4
|
-
// Ensure the MongoDB connection string is provided
|
5
|
-
if (!process.env.mongo) {
|
6
|
-
console.error('MongoDB connection string not provided. Set process.env.mongo.');
|
7
|
-
process.exit(1);
|
8
|
-
}
|
9
|
-
|
10
|
-
// Define the MongoDB URI
|
11
|
-
const uri = process.env.mongo;
|
12
|
-
|
13
|
-
// Define the database and collection name
|
14
|
-
const dbName = db;
|
15
|
-
const collectionName = collection;
|
16
|
-
|
17
|
-
async function main() {
|
18
|
-
// Create a new MongoClient
|
19
|
-
const client = new MongoClient(uri);
|
20
|
-
|
21
|
-
try {
|
22
|
-
// Connect to the MongoDB server
|
23
|
-
await client.connect();
|
24
|
-
console.log('Connected to the MongoDB server');
|
25
2
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
3
|
+
module.exports = async function (program, db, collection, pipeline, one = false) {
|
4
|
+
// Ensure the MongoDB connection string is provided
|
5
|
+
if (!process.env.mongo) {
|
6
|
+
console.error('MongoDB connection string not provided. Set process.env.mongo.');
|
7
|
+
process.exit(1);
|
8
|
+
}
|
9
|
+
|
10
|
+
// Define the MongoDB URI
|
11
|
+
const uri = process.env.mongo;
|
12
|
+
|
13
|
+
// Define the database and collection name
|
14
|
+
const dbName = db;
|
15
|
+
const collectionName = collection;
|
16
|
+
|
17
|
+
async function main() {
|
18
|
+
// Create a new MongoClient
|
19
|
+
const client = new MongoClient(uri);
|
20
|
+
|
21
|
+
try {
|
22
|
+
// Connect to the MongoDB server
|
23
|
+
await client.connect();
|
24
|
+
console.log('Connected to the MongoDB server');
|
25
|
+
|
26
|
+
// Select the database
|
27
|
+
const database = client.db(dbName);
|
28
|
+
|
29
|
+
// Select the collection
|
30
|
+
const collection = database.collection(collectionName);
|
31
|
+
|
32
|
+
// Perform the aggregation query
|
33
|
+
let result = await collection.aggregate(pipeline).toArray();
|
34
|
+
|
35
|
+
if (one) {
|
36
|
+
result = result[0];
|
37
|
+
}
|
38
|
+
|
39
|
+
// Process the result
|
40
|
+
console.log('Aggregation result:', result);
|
41
|
+
return result;
|
42
|
+
} catch (error) {
|
43
|
+
console.error('Error executing MongoDB aggregation:', error);
|
44
|
+
} finally {
|
45
|
+
// Close the MongoDB connection
|
46
|
+
await client.close();
|
47
47
|
}
|
48
|
+
}
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
}
|
50
|
+
// Execute the main function
|
51
|
+
return await main().catch(console.error);
|
52
|
+
};
|