/
db.getSiblingDB.txt
79 lines (51 loc) · 1.82 KB
/
db.getSiblingDB.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
=================
db.getSiblingDB()
=================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.getSiblingDB(<database>)
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``database``
- string
- The name of a MongoDB database.
:returns: A database object.
Used to return another database without modifying the
``db`` variable in the shell environment.
Example
-------
You can use :method:`db.getSiblingDB()` as an alternative to the ``use
<database>`` helper. This is particularly useful when writing scripts
using the :binary:`~bin.mongo` shell where the ``use`` helper is not
available. Consider the following sequence of operations:
.. code-block:: javascript
db = db.getSiblingDB('users')
db.active.count()
This operation sets the ``db`` object to point to the database named
``users``, and then returns a :doc:`count
</reference/method/db.collection.count>` of the collection named
``active``. You can create multiple ``db`` objects, that refer to
different databases, as in the following sequence of operations:
.. code-block:: javascript
users = db.getSiblingDB('users')
records = db.getSiblingDB('records')
users.active.count()
users.active.findOne()
records.requests.count()
records.requests.findOne()
This operation creates two ``db`` objects referring to different
databases (i.e. ``users`` and ``records``) and then returns a
:doc:`count </reference/method/db.collection.count>` and an
:doc:`example document </reference/method/db.collection.findOne>` from
one collection in that database (i.e. ``active`` and ``requests``
respectively.)