webfast 0.1.80 → 0.1.82
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
|
|
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
|
+
|
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 find query
|
33
|
+
let result = await collection.find(query).toArray();
|
34
|
+
|
35
|
+
if (one) {
|
36
|
+
result = result[0];
|
37
|
+
}
|
38
|
+
|
39
|
+
// Process the result
|
40
|
+
console.log('Query result:', result);
|
41
|
+
} catch (error) {
|
42
|
+
console.error('Error executing MongoDB query:', error);
|
43
|
+
} finally {
|
44
|
+
// Close the MongoDB connection
|
45
|
+
await client.close();
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
// Execute the main function
|
50
|
+
return await main().catch(console.error);
|
51
|
+
|
52
|
+
}
|
@@ -36,7 +36,19 @@ module.exports = async function(db,collection,query,one = false,array) {
|
|
36
36
|
const collection = await database.collection(collectionName);
|
37
37
|
|
38
38
|
// Perform the find query
|
39
|
-
|
39
|
+
if (array.sort != undefined && array.skip != undefined && array.limit != undefined) {
|
40
|
+
result = await collection.find(query).sort(array.sort).skip(array.skip).limit(array.limit).toArray();
|
41
|
+
} else if (array.limit != undefined && array.sort) {
|
42
|
+
result = await collection.find(query).sort(array.sort).limit(array.limit).toArray();
|
43
|
+
} else if (array.sort != undefined && array.skip != undefined) {
|
44
|
+
result = await collection.find(query).sort(array.sort).skip(array.skip).toArray();
|
45
|
+
} else if (array.sort != undefined) {
|
46
|
+
result = await collection.find(query).sort(array.sort).toArray();
|
47
|
+
} else if (array.limit != undefined) {
|
48
|
+
result = await collection.find(query).limit(array.limit).toArray();
|
49
|
+
} else {
|
50
|
+
result = await collection.find(query).toArray();
|
51
|
+
}
|
40
52
|
|
41
53
|
if (one == true) {
|
42
54
|
result = result[0];
|