diff --git a/mongodb.js b/mongodb.js index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..37662f0f05160caf7a8ee2de34eb663bbbb0a564 100644 --- a/mongodb.js +++ b/mongodb.js @@ -0,0 +1,22 @@ +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