Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb why do we need getSisterDB

Tags:

mongodb

While playing with mognodb console help, I found a db.getSisterDB() method.

And I am curious what is the purpose of this method. Looking through mongodb documentation and a quick google search did not yield satisfactory results.

By typing db.getSisterDb.help generates an error and typing db.getSisterDB gives the following definition of this method:

function ( name ){
    return this.getMongo().getDB( name );
}

which suggests that this is just a wrapper around getDB. My suggestion that it is used in to access databases in a replica set, but I would like to listen to a person who can give me a more thorough explanation.

like image 405
Salvador Dali Avatar asked Oct 27 '13 02:10

Salvador Dali


People also ask

Why do we need to use MongoDB?

Using MongoDB can provide many benefits to a software development team. Its flexible schema makes it easy to evolve and store data in a way that is easy for programmers to work with. MongoDB is also built to scale up quickly and supports all the main features of modern databases such as transactions.

What is DB getSiblingDB?

The db. getSiblingDB() method is used to provide access to the specified database. Syntax: db.getSiblingDB(<database>) Parameters: Name.

Can you have multiple databases in MongoDB?

A single MongoDB server can have multiple databases and a single MongoDB database can have multiple collections. You can use MongoDB Shell or MongoDB Compass to create a new database. MongoDB provides the use <database-name command to connect with the database.

What makes MongoDB the best?

High performance (speed) Thanks to the document model used in MongoDB, information can be embedded inside a single document rather than relying on expensive join operations from traditional relational databases. This makes queries much faster, and returns all the necessary information in a single call to the database.


1 Answers

In the shell, db is a reference to the current database. If you want to query against a different DB in the same mongod instance, the way to get a proper reference to it would be to use this method (which has an alias, more gender neutral getSiblingDB).

If you wanted to use the longer syntax, you could: db.getMongo().getDB(name) gets you the same thing as db.getSiblingDB(name) or db.getSisterDB(name) but the former is longer to type.

All of the above work the same way in standalone mongod as well as replica sets (and sharded clusters).

like image 175
Asya Kamsky Avatar answered Oct 21 '22 12:10

Asya Kamsky