xxxxxxxxxx
// Assuming you're using the official MongoDB Node.js driver
// Import MongoClient
const { MongoClient } = require('mongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'yourDatabaseName';
// Function to retrieve all users
async function showUsers() {
// Create a new MongoClient
const client = new MongoClient(url);
try {
// Connect to the MongoDB server
await client.connect();
// Access the database
const db = client.db(dbName);
// Access the users collection
const usersCollection = db.collection('users');
// Retrieve all users
const users = await usersCollection.find().toArray();
// Print or process the users as needed
console.log(users);
} catch (error) {
console.error('Error: ', error);
} finally {
// Close the connection
await client.close();
}
}
// Call the showUsers function
showUsers();