Skip to content

Commit e2915d9

Browse files
author
Dave
authored
DOCSP-16134 BACKPORT v5.0 add mongo.getDBs (#309)
* DOCSP-16134 add mongo.getDBs * add getDBNames * Links, TOC, Release note * change title * Review feedback * Review feedback * Review feedback
1 parent f2cdec2 commit e2915d9

File tree

3 files changed

+173
-14
lines changed

3 files changed

+173
-14
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
==================
2+
Mongo.getDBNames()
3+
==================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Description
14+
-----------
15+
16+
.. method:: Mongo.getDBNames()
17+
18+
Returns a list of available databases. :method:`Mongo.getDBNames()`
19+
calls the :dbcommand:`listDatabases` command.
20+
21+
The :method:`Mongo.getDBNames()` method doesn't take any parameters.
22+
23+
24+
Examples
25+
--------
26+
27+
List Databases
28+
~~~~~~~~~~~~~~
29+
30+
List the available databases for the current MongoDB instance:
31+
32+
.. code-block:: javascript
33+
34+
db.getMongo().getDBNames()
35+
36+
The :method:`db.getMongo()` method creates a connection to the
37+
instance. :method:`Mongo.getDBNames()` returns:
38+
39+
.. code-block:: javascript
40+
:copyable: false
41+
42+
[ 'admin', 'config', 'local', 'test' ]
43+
44+
Map Database List to Another Method
45+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46+
47+
Use :method:`Mongo.getDBNames()` to get a list of collections:
48+
49+
.. code-block:: javascript
50+
51+
db.getMongo().getDBNames().map(
52+
name => db.getSiblingDB( name ).getCollectionNames()
53+
)
54+
55+
Example output:
56+
57+
.. code-block:: javascript
58+
:copyable: false
59+
60+
[
61+
[ 'system.users', 'system.keys', 'system.version' ],
62+
[
63+
'settings',
64+
'tenantMigrationRecipients',
65+
'system.sessions',
66+
'transactions',
67+
'external_validation_keys',
68+
'image_collection',
69+
'tenantMigrationDonors',
70+
'system.indexBuilds'
71+
],
72+
[
73+
'replset.minvalid',
74+
'system.views',
75+
'oplog.rs',
76+
'replset.initialSyncId',
77+
'startup_log',
78+
'system.replset',
79+
'system.rollback.id',
80+
'replset.oplogTruncateAfterPoint',
81+
'replset.election',
82+
'system.tenantMigration.oplogView'
83+
],
84+
[
85+
'feedback',
86+
'inventory',
87+
'engineers',
88+
'clothes'
89+
]
90+
]
91+
92+
- :method:`Mongo.getDBNames()` returns a list of databases.
93+
- ``map`` defines a function that iterates over the list of databases.
94+
Each iteration of ``map``:
95+
96+
- assigns a database to the ``name`` variable,
97+
- connects to the database currently stored in ``name`` using
98+
:method:`db.getSiblingDB()`,
99+
- returns the collections in the current database using
100+
:method:`db.getCollectionNames()`.
101+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
==============
2+
Mongo.getDBs()
3+
==============
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Description
14+
-----------
15+
16+
.. method:: Mongo.getDBs()
17+
18+
Returns information about all available databases.
19+
:method:`Mongo.getDBs()` uses the :dbcommand:`listDatabases`
20+
command.
21+
22+
The :method:`Mongo.getDBs()` method doesn't take any parameters.
23+
24+
25+
Example
26+
-------
27+
28+
To list the available databases and metadata for the local MongoDB
29+
instance:
30+
31+
.. code-block:: javascript
32+
33+
db.getMongo().getDBs()
34+
35+
The :method:`db.getMongo()` method returns the connection to the
36+
current MongoDB instance. The :method:`Mongo.getDBs()` output
37+
resembles:
38+
39+
.. code-block:: javascript
40+
:copyable: false
41+
:emphasize-lines: 2-7
42+
43+
{
44+
databases: [
45+
{ name: 'admin', sizeOnDisk: Long("225280"), empty: false },
46+
{ name: 'config', sizeOnDisk: Long("212992"), empty: false },
47+
{ name: 'local', sizeOnDisk: Long("2400256"), empty: false },
48+
{ name: 'test', sizeOnDisk: Long("303104"), empty: false }
49+
],
50+
totalSize: Long("3141632"),
51+
totalSizeMb: Long("2"),
52+
ok: 1,
53+
'$clusterTime': {
54+
clusterTime: Timestamp({ t: 1640186473, i: 1 }),
55+
signature: {
56+
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
57+
keyId: Long("0")
58+
}
59+
},
60+
operationTime: Timestamp({ t: 1640186473, i: 1 })
61+
}
62+
63+
The databases are listed in the highlighted lines.
64+

source/reference/method/js-connection.txt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,59 @@ Connection Methods
1717
:header-rows: 1
1818

1919
* - Name
20-
2120
- Description
2221

2322
* - :doc:`/reference/method/connect`
24-
2523
- Connects to a MongoDB instance and to a specified database on that instance.
2624

2725
* - :method:`Mongo()`
28-
2926
- Creates a new connection object.
3027

3128
* - :method:`Mongo.getDB()`
32-
3329
- Returns a database object.
3430

35-
* - :method:`Mongo.getReadPrefMode()`
31+
* - :method:`Mongo.getDBNames()`
32+
- Returns a list of databases.
33+
34+
* - :method:`Mongo.getDBs()`
35+
- Returns a document with a list of databases and metadata.
3636

37+
* - :method:`Mongo.getReadPrefMode()`
3738
- Returns the current read preference mode for the MongoDB connection.
3839

3940
* - :method:`Mongo.getReadPrefTagSet()`
40-
4141
- Returns the read preference tag set for the MongoDB connection.
4242

4343
* - :method:`Mongo.setCausalConsistency()`
44-
4544
- Enables or disables causal consistency on the connection object.
4645

4746
* - :method:`Mongo.setReadPref()`
48-
4947
- Sets the :term:`read preference` for the MongoDB connection.
5048

5149
* - :method:`Mongo.startSession()`
52-
5350
- Starts a session on the connection object.
5451

5552
* - :method:`Mongo.watch()`
56-
5753
- Opens a :ref:`change stream cursor <changeStreams>` for a deployment
5854
to report on all its non-``system`` collections across all its
5955
databases, excluding the internal ``admin``, ``local``, and
6056
``config`` databases.
61-
6257

6358
* - :method:`Session`
64-
6559
- The session object.
6660

6761
* - :method:`SessionOptions`
68-
6962
- The options object for the session.
7063

71-
7264
.. toctree::
7365
:titlesonly:
7466
:hidden:
7567

7668
/reference/method/connect
7769
/reference/method/Mongo
7870
/reference/method/Mongo.getDB
71+
/reference/method/Mongo.getDBNames
72+
/reference/method/Mongo.getDBs
7973
/reference/method/Mongo.getReadPrefMode
8074
/reference/method/Mongo.getReadPrefTagSet
8175
/reference/method/Mongo.setCausalConsistency

0 commit comments

Comments
 (0)