Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can link to two distinct MongoDB databases in your Nuxt 3/ Vue 3 application by following these steps:

  1. Install the MongoDB driver for Node.js using the npm package manager:
npm install mongodb
  1. In your application code, import the MongoClient class from the MongoDB driver:
import { MongoClient } from 'mongodb';
  1. Create two separate MongoClient instances, each with its own connection string and database name:
const client1 = await MongoClient.connect('<first connection string>');
const db1 = client1.db('<first database name>');

const client2 = await MongoClient.connect('<second connection string>');
const db2 = client2.db('<second database name>');
  1. Use the database objects to perform CRUD operations on the respective databases:
const collection1 = db1.collection('<collection name>');
const collection2 = db2.collection('<collection name>');

// Query collection1:
const documents1 = await collection1.find({}).toArray();

// Query collection2:
const documents2 = await collection2.find({}).toArray();

You can call these database connections and database names in your Vuex store or even define them globally via plugins to be used throughout your application.