w3resource

MongoDB: db.getSiblingDB() method

db.getSiblingDB

The db.getSiblingDB() method is used to provide access to the specified database.

Syntax:

db.getSiblingDB(<database>)

Parameters:

Name Description Required /
Optional
Type
database The name of a MongoDB database. Required string

Used to return another database without modifying the db variable in the shell environment.

Example: MongoDB: db.getSiblingDB() method

This following operation sets the db object to point to the database named test, and then returns a count of the collection named restaurants.

test= db.getSiblingDB('test')
test.restaurants.count()

Output:

> test= db.getSiblingDB('test')
test
> test.restaurants.count()
25359

This following operation creates two db objects referring to different databases (i.e. test and myinfo) and then returns a count and an example document from one collection in that database (i.e. restaurants and invoice restsrespectively.)

test= db.getSiblingDB('test')
myinfo= db.getSiblingDB('myinfo')

test.restaurants.count()
test.restaurants.findOne()

myinfo.invoice.count()
myinfo.invoice.findOne()

Output:

> test= db.getSiblingDB('test')
test
> myinfo= db.getSiblingDB('myinfo')
myinfo
> test.restaurants.count()
25359
> test.restaurants.findOne()
{
        "_id" : ObjectId("55c3043ab165fa6355ec5c89"),
        "address" : {
                "building" : "1007",
                "coord" : [
                        -73.856077,
                        40.848447
                ],
                "street" : "Morris Park Ave",
                "zipcode" : "10462"
        },
        "borough" : "Bronx",
        "cuisine" : "Bakery",
        "grades" : [
                {
                        "date" : ISODate("2014-03-03T00:00:00Z"),
                        "grade" : "A",
                        "score" : 2
                },
                {
                        "date" : ISODate("2013-09-11T00:00:00Z"),
                        "grade" : "A",
                        "score" : 6
                },
                {
                        "date" : ISODate("2013-01-24T00:00:00Z"),
                        "grade" : "A",
                        "score" : 10
                },
                {
                        "date" : ISODate("2011-11-23T00:00:00Z"),
                        "grade" : "A",
                        "score" : 9
                },
                {
                        "date" : ISODate("2011-03-10T00:00:00Z"),
                        "grade" : "B",
                        "score" : 14
                }
        ],
        "name" : "Morris Park Bake Shop",
        "restaurant_id" : "30075445"
}
> myinfo.invoice.count()
8
> myinfo.invoice.findOne()
{
        "_id" : 1,
        "item" : "doz",
        "qty" : 20,
        "rate" : 10,
        "inv_date" : "02/02/2014"
}

Retrieve the restaurants data from here

Previous: db.getProfilingStatus() method
Next: db.help() method



Share this Tutorial / Exercise on : Facebook and Twitter