You can read documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB游乐场中使用MongoDB CRUD运算符读取集合中的文档:
Use the findOne() method to read one document.使用findOne()方法读取一个文档。Use the find() method to read more than one document.使用find()方法读取多个文档。
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游乐场。
Prerequisites先决条件
If you have not done so already, you must complete the following prerequisites before you can read documents with a MongoDB Playground:如果您还没有这样做,则必须完成以下先决条件,然后才能使用MongoDB游乐场阅读文档:
Create a connection to a MongoDB deployment.创建与MongoDB部署的连接。Activate the connection to the MongoDB deployment.激活与MongoDB部署的连接。Open a MongoDB Playground.打开一个MongoDB游乐场。Create Documents with VS Code使用VS Code创建文档or create documents in a collection using a different method.或者使用不同的方法在集合中创建文档。
Read One Document阅读一个文档
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.如果多个文档满足查询,则此方法根据反映磁盘上文档顺序的自然顺序返回第一个文档。
To learn more about 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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.要运行游乐场,请按游乐场视图右上角的“播放”按钮。VS Code扩展拆分游乐场,并在“游乐场Results.json”窗格中输出游乐场的结果。如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。
You may edit any JSON document returned from a findOne() or find() operation.您可以编辑从findOne()或find()操作返回的任何JSON文档。
At the top of this document, click Edit Document. VS Code Extension opens it as an editable EJSON document titled在此文档顶部,单击“编辑文档”。VS Code扩展将其作为标题为<database>.<collection>:"<_id>".json.<database>.<collection>:"<_id>".json的可编辑EJSON文档打开。Make any edits you require.根据需要进行任何编辑。Press按Ctrl+S(Windows/Linux)或Cmd+S将编辑后的文档保存到MongoDB数据库。Ctrl + S(Windows/Linux) orCmd + Sto save the edited document to the MongoDB database.If the update succeeds, VS Code Extension confirms that the database has stored the change.如果更新成功,VS Code扩展将确认数据库已存储更改。If the update results in an error, VS Code Extension displays it.如果更新导致错误,VS Code扩展将显示它。
Example示例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB游乐场开始,如果加载了模板游乐场,请清除它。
The following example:以下示例:
Switches to the切换到testdatabase.test数据库。Reads one document in the读取test.salescollection that matches the query.test.sales集合中与查询匹配的一个文档。
use("test");
db.sales.findOne(
{ "_id" : 1 },
{ "_id" : 0 }
);
When you press the Play Button, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。如果手动移动游乐场结果,VS Code延伸将在该选项卡中显示结果。
{
item: 'abc',
price: 10,
quantity: 2,
date: 2014-03-01T08:00:00.000Z
}Read Many Documents阅读许多文档
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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.要运行游乐场,请按游乐场视图右上角的“播放按钮”。VS Code扩展拆分游乐场,并在“游乐场Results.json”窗格中输出游乐场的结果。如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。
Example示例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB游乐场开始,如果加载了模板游乐场,请清除它。
The following example:以下示例:
Switches to the切换到testdatabase.test数据库。Reads all documents in the读取test.salescollection that match the query.test.sales集合中与查询匹配的所有文档。
use("test");
db.sales.find(
{ "item" : "abc" },
{ "price" : 1 }
);
When you press the Play Button, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。如果手动移动游乐场结果,VS Code延伸将在该选项卡中显示结果。
[
{
_id: 2,
price: 10
},
{
_id: 6,
price: 10
},
{
_id: 9,
price: 10
},
{
_id: 1,
price: 10
}
]Read Documents with a Cursor使用游标读取文档
In VS Code, cursors automatically log up to 20 documents by default. If there are more results, you can access them using the cursor's iteration methods or convert the cursor to an array.在VS Code中,默认情况下游标会自动记录多达20个文档。如果有更多结果,您可以使用游标的迭代方法访问它们,或者将游标转换为数组。
To convert an entire cursor to an array, use the following syntax in your Playground:要将整个游标转换为数组,请在游乐场中使用以下语法:
let cursor = db.collection.find({ <query> });
cursor.toArray();
Example示例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB游乐场开始,如果加载了模板游乐场,请清除它。
The following example:以下示例:
Switches to the切换到testdatabase.test数据库。Reads all documents from the从test.salescollection that match the query.test.sales集合中读取与查询匹配的所有文档。Returns the entire cursor as an array.将整个游标作为数组返回。
use("test");
let cursor = db.sales.find(
{ "price": { $gte: 10 } },
{ "price": 1 }
);
cursor.toArray();
When you press the Play Button, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。If you disabled split-view, VS Code Extension outputs the following document in a new tab. 如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档。If you manually move your playground results, VS Code Extension displays the results in that tab.如果手动移动操场结果,VS Code扩展将在该选项卡中显示结果。
[
{
"_id": 2,
"price": 10
},
{
"_id": 3,
"price": 20
},
{
"_id": 6,
"price": 10
},
{
"_id": 9,
"price": 10
}
]Learn More了解更多
For more detailed documentation on cursor methods and behaviors, see:有关游标方法和行为的更多详细文档,请参阅: