Quantcast
Channel: SQL Server Blog
Viewing all articles
Browse latest Browse all 1849

MongoDB -Access different databases and Collections

$
0
0

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

The current database is Test and accessing PP database temporarily from  Test.

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

screen1

Access restaurant collection of PP database

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

screen2

Output:-

screen4



Viewing all articles
Browse latest Browse all 1849

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>