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
- first
- restaurants
- second
Accessing PP database temporarily from Test database
To access ‘restaurants’ collection of PP database
db.getSiblingDB('PP').restaurants.find().pretty(); OR db.getSiblingDB('PP').restaurants.findOne();
Output:-