Docs HomeMongoDB for VS Code

Navigate Your Data浏览数据

Once you connect to your deployment using MongoDB for VS Code, use the left navigation to:使用MongoDB for VS Code连接到部署后,使用左侧导航:

  • Explore your databases, collections, read-only views, and documents.浏览数据库、集合、只读视图和文档。
  • Create new databases and collections.创建新的数据库和集合。
  • Drop databases and collections.删除数据库和集合。
Image showing deployment navigation
Note

If your deployment requires authentication, your database user privileges may affect the actions you can perform using MongoDB for VS Code.如果您的部署需要身份验证,那么您的数据库用户权限可能会影响您可以使用MongoDB for VS Code执行的操作。

View Databases and Collections查看数据库和集合

When you expand an active connection, MongoDB for VS Code shows the databases in that deployment.当您扩展活动连接时,MongoDB for VS Code会显示该部署中的数据库。

  • Click the name of a database to view the collections it contains.单击数据库的名称以查看其包含的集合。
  • Click the name of a collection to view its documents, schema, and indexes.单击集合的名称可以查看其文档、架构和索引。
Note

MongoDB for VS Code closes all documents when you close Visual Studio Code.关闭Visual Studio Code时,MongoDB for VS Code将关闭所有文档。

View Collection Documents and Schema查看集合文档和架构

When you expand a collection, MongoDB for VS Code displays the number of documents next to the Documents label in the navigation panel.当您展开集合时,MongoDB for VS Code会在导航面板中的“文档”标签旁边显示文档数。

Documents文件

When you expand a collection's documents, MongoDB for VS Code lists the _id of each document in the collection. 当您展开集合的文档时,MongoDB for VS Code会列出集合中每个文档的_idClick an _id value to open that document in Visual Studio Code and view its contents.单击_id值以在Visual Studio Code中打开该文档并查看其内容。

You may edit this single document:您可以编辑此单个文档:

  1. At the top of this document, click Edit Document. 在此文档顶部,单击“编辑文档”。MongoDB for VS Code opens it as an editable EJSON document titled <database>.<collection>:"<_id>".json.MongoDB for VS Code将其作为标题为<database>.<collection>:"<_id>".json的可编辑EJSON文档打开。
  2. Make any edits you require.进行所需的任何编辑。
  3. Press Ctrl + S (Windows/Linux) or Cmd + S to save the edited document to the MongoDB database.Ctrl+S(Windows/Linux)或Cmd+S将编辑后的文档保存到MongoDB数据库中。

    • If the update succeeds, MongoDB for VS Code confirms that the database has stored the change.如果更新成功,MongoDB for VS Code将确认数据库已存储更改。
    • If the update results in an error, MongoDB for VS Code displays it.如果更新导致错误,MongoDB for VS Code会显示它。
Important

Users must have the listCollections permission in order to view a collection's documents.用户必须具有listCollections权限才能查看集合的文档。

To view all of the collection's documents in an array, you can:要查看数组中集合的所有文档,您可以:

  1. Right-click a collection.在集合上单击鼠标右键。
  2. Click View Documents.单击“查看文档”。
Note

You can open a JavaScript Playground pre-configured to search a collection by hovering over the Documents label in the navigation panel and clicking the icon that appears.您可以通过将鼠标悬停在导航面板中的“文档”标签上并单击显示的图标,打开预先配置为搜索集合的JavaScript Playground

Schema

Your collection's schema defines the fields and data types within the collection. 集合的架构定义了集合中的字段和数据类型。Due to MongoDB's flexible schema model, different documents in a collection may contain different fields, and data types may vary within a field. 由于MongoDB灵活的模式模型,集合中的不同文档可能包含不同的字段,并且字段中的数据类型可能不同。MongoDB can enforce schema validation to ensure your collection documents have the same shape.MongoDB可以强制执行模式验证,以确保您的集合文档具有相同的形状。

When you expand a collection's schema, MongoDB for VS Code lists the fields which appear in that collection's documents. 当您展开集合的模式时,MongoDB for VS Code会列出该集合文档中出现的字段。If a field exists in all documents and its type is consistent throughout the collection, MongoDB for VS Code displays an icon indicating that field's data type. 如果一个字段存在于所有文档中,并且其类型在整个集合中是一致的,则MongoDB for VS Code会显示一个图标,指示该字段的数据类型。Hover over the field name for a text description of the field's data type.将鼠标悬停在字段名称上,可以看到字段数据类型的文本描述。

Manage Indexes管理索引

Your collections's indexes are listed under the Indexes heading. 集合的索引列在“索引”标题下。When you expand an index, each index key appears with an icon designating its type. 展开索引时,每个索引键都会显示一个指定其类型的图标。Index key types include:索引键类型包括:

  • Ascending
  • Descending
  • Geospatial (2d, 2dsphere, geoHaystack)
  • Text
  • Hashed
Note

You can open a MongoDB Playground pre-configured to create an index by hovering over the Indexes label in the navigation panel and clicking the icon that appears.您可以通过将鼠标悬停在导航面板中的“索引”标签上并单击显示的图标,打开预先配置为创建索引的MongoDB Playground

Tip

See also: 另请参阅:

To learn more about MongoDB indexes, see the server manual.要了解有关MongoDB索引的更多信息,请参阅服务器手册

Create a New Database创建新数据库

When you create a new database, you must populate it with an initial collection.创建新数据库时,必须使用初始集合填充该数据库。

  1. Hover over the connection for the deployment where you want your database to exist.将鼠标悬停在希望数据inventory在的部署的连接上。
  2. Click the icon or right-click and select Add Database. 单击图标或右键单击并选择“添加数据库”。MongoDB for VS Code opens a new tab with const variables for database and collection names.MongoDB for VS Code打开了一个新的选项卡,其中包含数据库和集合名称的const变量。
  3. Update the database and collection variables with the names for your database and collection.使用databasecollection的名称更新数据库和集合变量。
  4. Click the button located at the top right of the tab to execute the script. 单击选项卡右上角的按钮以执行脚本。If the database and collection do not already exist, they are created.如果数据库和集合尚不存在,则会创建它们。

Create a New Collection创建新集合

  1. Hover over the database name where you want your collection to exist.将鼠标悬停在希望集合所在的数据库名称上。
  2. Right-click the database name and select Add Collection. 右键单击数据库名称,然后选择“添加集合”。MongoDB for VS Code opens a new tab with const variables for database and collection names.MongoDB for VS Code打开了一个新的选项卡,其中包含数据库和集合名称的常量变量。
  3. Update the collection variable with your new collection name.使用新的集合名称更新collection变量。
  4. Click the button located at the top right of the tab to execute the script. 单击选项卡右上角的按钮以执行脚本。The collection is created in the specified database.集合是在指定的数据库中创建的。

Drop a Database or Collection删除数据库或集合

Important

Dropping data from MongoDB is an irreversible process. 从MongoDB中删除数据是一个不可逆转的过程。Take caution to only drop data you are sure you want to delete, and backup your data as necessary.请注意,只删除您确定要删除的数据,并在必要时备份数据。

Dropping a database also drops all collections and documents within that database.删除数据库也会删除该数据库中的所有集合和文档。

To drop a database or collection:要删除数据库或集合,请执行以下操作:

  1. Right-click the target database or collection.右键单击目标数据库或集合。
  2. Click Drop Database or Drop Collection.单击“删除数据库”或“删除集合”。
  3. In the prompt, type the name of the target database or collection.在提示中,键入目标数据库或集合的名称。
  4. Press the enter key.按回车键。

Refresh Data刷新数据

You can refresh a deployment, database, or collection at any time to re-query your deployment and populate MongoDB for VS Code with the most up-to-date data.您可以随时刷新部署、数据库或集合,以重新查询您的部署,并使用最新数据填充MongoDB for VS Code。

To refresh:若要刷新:

  1. Right-click the target deployment, database, or collection.右键单击目标部署、数据库或集合。
  2. Click Refresh.单击“刷新”。