Skip to content
Snippets Groups Projects

Resolve "Configure MongoDB Connection"

1 file
+ 22
0
Compare changes
  • Side-by-side
  • Inline
+ 22
0
const { MongoClient, Db } = require('mongodb');
const url = 'mongodb://localhost:27017/todo';
async function createCollections() {
try {
const client = new MongoClient(url);
await client.connect();
// Check if the "todo" database exists and create it if not
const db = client.db('todo');
await db.createCollection('users'); // Check if collection "users" exists and create it if it doesn't exist
await db.createCollection('todos'); // Check if collection "todos" exists and create it if it doesn't exist
await db.createCollection('global'); // Check if collection "global" exists and create it if it doesn't exist
console.log('Collections created successfully!');
await client.close();
} catch (error) {
console.error('Error creating collections:', error);
}
}
createCollections();
\ No newline at end of file
Loading