You perform database operations by using the handle db
, which is built into MongoDB. Many operations can only be applied on one database. Therefore, to perform operations on other databases, you need to change the db
handle to point to a different database.
To switch the current database, you use the db.getSiblingDB(database)
method or use <database>
. For example, both of the following methods switch the current database handle to testDB
:
db = db.getSiblingDB('testDB')
use testDB
Either one is acceptable and sets the value of db
to the database specified. You can then use db
to manage the new current database.