Advertisement
nodejsdeveloperskh

mongodb-cluster-bitnami

Mar 31st, 2025
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { MongoClient } from 'mongodb';
  2.  
  3. const uri =
  4.   'mongodb://my:my@localhost:27017,localhost:27018,localhost:27019/my?replicaSet=rs0';
  5.  
  6. try {
  7.   const client = new MongoClient(uri);
  8.  
  9.   await client.connect();
  10.   console.log('Connected to MongoDB Replica Set!');
  11.  
  12.   const db = client.db('my');
  13.   const collection = db.collection('testcollection');
  14.  
  15.   const result = await collection.insertOne({
  16.     message: 'Hello, MongoDB Replica Set!',
  17.   });
  18.   console.log('Inserted Document:', result.insertedId);
  19.  
  20.   await client.close();
  21. } catch (error) {
  22.   console.error('Error connecting to MongoDB:', error);
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement