Docs Home / Node.js Driver

Databases and Collections数据库和集合

Overview概述

In this guide, you can learn how to interact with MongoDB databases and collections by using the Node.js driver.在本指南中,您可以学习如何使用Node.js驱动程序与MongoDB数据库和集合进行交互。

MongoDB organizes data into a hierarchy of the following levels:MongoDB将数据组织成以下级别的层次结构:

  • Databases数据库: Top-level data structures in a MongoDB deployment that store collections.:MongoDB部署中存储集合的顶级数据结构。
  • Collections集合: Groups of MongoDB documents. They are analogous to tables in relational databases.:MongoDB文档组。它们类似于关系数据库中的表。
  • 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.:存储字符串、数字、日期和其他嵌入式文档等文字数据的单元。有关文档字段类型和结构的更多信息,请参阅MongoDB Server手册中的Documents条目。

Access a Database访问数据库

You can access a database by calling the db() method on a MongoClient instance.您可以通过在MongoClient实例上调用db()方法来访问数据库。

The following example accesses a database named "test_database":以下示例访问名为"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.您可以通过在Db实例上调用collection()方法来访问集合。

The following example accesses a collection named "test_collection":以下示例访问名为"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.如果提供的集合名称在数据库中不存在,MongoDB会在您首次向其中插入数据时隐式创建该集合。

Create a Collection创建集合

To explicitly create a collection, call the createCollection() method on a Db instance.要显式创建集合,请在Db实例上调用createCollection()方法。

The following example creates a collection named "example_collection":以下示例创建了一个名为"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.您可以通过将CreateCollectionOptions实例传递给createCollection()方法来指定集合选项,如最大大小和文档验证规则。有关可选参数的完整列表,请参阅MongoDB Server手册中的create命令条目。

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.您可以通过在Db实例上调用listCollections()方法来查询数据库中的集合列表。

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:要仅查询数据库中集合的名称,请将nameOnly选项传递给listCollections()方法,并将其值设置为true,如下代码所示:

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.您可以通过在collection实例上调用drop()方法来删除集合。

The following example deletes the "test_collection" collection:以下示例删除"test_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.仅当不再需要集合中的数据时,才删除该集合。

Delete a Database删除数据库

You can delete a database from your cluster by calling the dropDatabase() method on a Database instance.您可以通过在Database实例上调用dropDatabase()方法从集群中删除数据库。

The following example deletes the "test_database" database:以下示例删除"test_database"数据库:

const database = client.db("test_database");
await database.dropDatabase();

Warning

Dropping a Database Deletes All Data in the Database删除数据库会删除数据库中的所有数据

Dropping a database permanently deletes all collections, documents, and indexes within that database.删除数据库会永久删除该数据库中的所有集合、文档和索引。

Drop a database 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:要了解有关本指南中讨论的任何类型或方法的更多信息,请参阅以下API文档: