Databases and Collections
On this page
Overview
In this guide, you can learn how to interact with MongoDB databases and collections by using the Node.js driver.
MongoDB organizes data into a hierarchy of the following levels:
Databases: Top-level data structures in a MongoDB deployment that store collections.
Collections: Groups of MongoDB documents. They are analogous to tables in relational databases.
Documents: Units that store literal data such as strings, numbers, dates, and other embedded documents. For more information about document field types and structure, see the Documents entry in the MongoDB Server manual.
Access a Database
You can access a database by calling the db()
method on a
MongoClient
instance.
The following example accesses a database named "test_database"
:
const database = client.db("test_database");
Access a Collection
You can access a collection by calling the collection()
method on a Db
instance.
The following example accesses a collection named "test_collection"
:
const collection = database.collection("test_collection");
Tip
If the provided collection name does not already exist in the database, MongoDB implicitly creates the collection when you first insert data into it.
Create a Collection
To explicitly create a collection, call the createCollection()
method on
a Db
instance.
The following example creates a collection named "example_collection"
:
const createColl = await database.createCollection("example_collection");
You can specify collection options, such as maximum size and document
validation rules, by passing a CreateCollectionOptions
instance to the
createCollection()
method. For a full list of
optional parameters, see the create command
entry in the MongoDB Server manual.
Get a List of Collections
You can query for a list of collections in a database by calling the
listCollections()
method on a Db
instance.
The following example lists all collections in a database:
const colls = database.listCollections(); for await (const doc of colls) { console.log(doc) }
{ name: 'example_collection', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID('...') }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } } { name: 'test_collection', type: 'collection', options: {}, info: { readOnly: false, uuid: new UUID('...') }, idIndex: { v: 2, key: { _id: 1 }, name: '_id_' } }
To query for only the names of the collections in the database, pass
the nameOnly
option to the listCollections()
method and set its
value to true
, as shown in the following code:
const names = database.listCollections({}, { nameOnly: true }); for await (const doc of names) { console.log(doc) }
{ name: 'example_collection', type: 'collection' } { name: 'test_collection', type: 'collection' }
Tip
For more information about iterating over a cursor, see the Access Data From a Cursor guide.
Delete a Collection
You can delete a collection by calling the drop()
method on a
Collection
instance.
The following example deletes the "test_collection"
collection:
const collectionToDelete = database.collection("test_collection"); await collectionToDelete.drop();
Warning
Dropping a Collection Deletes All Data in the Collection
Dropping a collection from your database permanently deletes all documents and all indexes within that collection.
Drop a collection only if the data in it is no longer needed.
API Documentation
To learn more about any of the types or methods discussed in this guide, see the following API documentation: