Blog Post

MongoDB -Access different databases and Collections

,

You can use db.getSiblingDB() method to access another database without switching the database.

To List Collections of PP database and query collection named “restaurants”

db.getSiblingDB('PP').getCollectionNames()

The PP Database has three collections

  1. first
  2. restaurants
  3. second

Accessing PP database temporarily from Test database

screen1

To access ‘restaurants’ collection of PP database

db.getSiblingDB('PP').restaurants.find().pretty();
OR
db.getSiblingDB('PP').restaurants.findOne();

screen2

Output:-

screen4

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating