-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-connection.js
More file actions
35 lines (28 loc) · 992 Bytes
/
test-connection.js
File metadata and controls
35 lines (28 loc) · 992 Bytes
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
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://seruji:susgayjojo@cluster0.npqmt4i.mongodb.net/laura?retryWrites=true&w=majority&appName=Cluster0";
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
console.log('Attempting to connect to MongoDB...');
await client.connect();
console.log('Connected to MongoDB server');
const db = client.db('laura');
console.log('Accessing database:', db.databaseName);
await db.command({ ping: 1 });
console.log("Successfully connected to MongoDB!");
// Try to list collections
const collections = await db.listCollections().toArray();
console.log('Available collections:', collections.map(c => c.name));
} catch (error) {
console.error("Error connecting to MongoDB:", error);
} finally {
await client.close();
}
}
run();