forked from abdularhamkhan/GameSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-test.js
More file actions
73 lines (63 loc) · 2.14 KB
/
Copy pathdb-test.js
File metadata and controls
73 lines (63 loc) · 2.14 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Test script to verify MongoDB connection
const { MongoClient } = require('mongodb');
// Simplified connection URI
const uri = "mongodb+srv://GameSpaceDev:GameSpaceDev1234@gamespacecluster.79wzx7g.mongodb.net/GameSpaceDB";
const dbName = "GameSpaceDB";
// Minimal connection options
const options = {
serverSelectionTimeoutMS: 5000
};
async function testConnection() {
console.log('Attempting to connect to MongoDB...');
let client;
try {
// Connect to the MongoDB server
client = new MongoClient(uri, options);
await client.connect();
console.log('✅ Successfully connected to MongoDB Atlas!');
// Verify database access
const db = client.db(dbName);
console.log(`✅ Successfully accessed the "${dbName}" database!`);
// List all collections in the database
const collections = await db.listCollections().toArray();
console.log('Collections in the database:');
if (collections.length === 0) {
console.log(' No collections found. Database may be empty.');
} else {
collections.forEach(collection => {
console.log(` - ${collection.name}`);
});
}
// Check if Users collection exists and count documents
const usersCollection = db.collection("Users");
const userCount = await usersCollection.countDocuments();
console.log(`Users collection has ${userCount} document(s)`);
if (userCount > 0) {
// Show sample user (without password)
const sampleUser = await usersCollection.findOne({}, { projection: { password: 0 } });
console.log('Sample user:', sampleUser);
}
return true;
} catch (error) {
console.error('❌ Error connecting to MongoDB:', error);
return false;
} finally {
// Close the connection
if (client) {
await client.close();
console.log('Connection closed.');
}
}
}
// Run the test
testConnection()
.then(success => {
if (success) {
console.log('\nDatabase connection test completed successfully!');
} else {
console.log('\nDatabase connection test failed!');
}
})
.catch(err => {
console.error('Unexpected error during test:', err);
});