Read Documents读取文档

On this page本页内容

You can read documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB游乐场中使用MongoDB CRUD运算符读取集合中的文档:

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游乐场

If you have not done so already, you must complete the following prerequisites before you can read documents with a MongoDB Playground:如果您还没有这样做,您必须先完成以下先决条件,然后才能使用MongoDB Playground读取文档:

To read one document, use the following syntax in your Playground:要读取一个文档,请在您的游乐场中使用以下语法:

db.collection.findOne(
   { <query> },
   { <projection> }
)

If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.如果多个文档满足查询,该方法将根据反映磁盘上文档顺序的自然顺序返回第一个文档。

For a detailed description of this method's parameters, see findOne() in the MongoDB Manual.有关此方法参数的详细说明,请参阅MongoDB手册中的findOne()

To run your Playground, press the Play Button at the top right of the Playground View. 要运行游乐场,请按游乐场视图右上角的播放按钮MongoDB for VS Code outputs the results of your playground to the Output view in Visual Studio Code.MongoDB for VS Code将游乐场的结果输出到Visual Studio Code中的输出视图。

To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要使用此示例,请从一个空白的MongoDB Playground开始,如果已加载,则清除模板Playgrown。

The following example:以下示例:

  1. Switches to the test database.切换到test数据库。
  2. Reads one document in the test.sales collection that matches the query.读取test.sales集合中与查询匹配的一个文档。
use("test");
db.sales.findOne(
  { "_id" : 1 },
  { "_id" : 0 }
);

When you press the Play Button, this operation outputs the following document to the Output view in Visual Studio Code:按下“播放”按钮时,此操作将以下文档输出到Visual Studio Code中的输出视图:

{
  item: 'abc',
  price: 10,
  quantity: 2,
  date: 2014-03-01T08:00:00.000Z
}

To read many documents, use the following syntax in your Playground:要读取许多文档,请在您的游乐场中使用以下语法:

db.collection.find(
   { <query> },
   { <projection> }
)

For a detailed description of this method's parameters, see find() in the MongoDB Manual.有关此方法参数的详细说明,请参阅MongoDB手册中的find()

To run your Playground, press the Play Button at the top right of the Playground View. 要运行游乐场,请按游乐场视图右上角的“播放”按钮。MongoDB for VS Code outputs the results of your playground to the Output view in Visual Studio Code.MongoDB for VS Code将游乐场的结果输出到Visual Studio Code中的输出视图。

To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要使用此示例,请从一个空白的MongoDB Playground开始,如果已加载,则清除模板Playgrown。

The following example:以下示例:

  1. Switches to the test database.切换到test数据库。
  2. Reads all documents in the test.sales collection that match the query.读取test.sales集合中与查询匹配的所有文档。
use("test");
db.sales.find(
  { "item" : "abc" },
  { "price" : 1 }
);

When you press the Play Button, this operation outputs the following document to the Output view in Visual Studio Code:按下“播放”按钮时,此操作将以下文档输出到Visual Studio Code中的输出视图:

[
  {
    _id: 2,
    price: 10
  },
  {
    _id: 6,
    price: 10
  },
  {
    _id: 9,
    price: 10
  },
  {
    _id: 1,
    price: 10
  }
]
Create DocumentsUpdate Documents