Docs Home → MongoDB for VS Code
Read Documents读取文档
On this page本页内容
You can read documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:您可以在MongoDB Playground中使用MongoDB CRUD运算符读取集合中的文档:
Use the findOne() method to read one document.使用findOne()
方法读取一个文档。Use the find() method to read more than one document.使用find()
方法读取多个文档。
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.您可以通过将鼠标悬停在导航面板中的Documents标签上并单击显示的图标,打开预先配置为搜索集合的JavaScript Playground。
Prerequisites先决条件
If you have not done so already, you must complete the following prerequisites before you can read documents with a MongoDB Playground:如果您还没有这样做,您必须完成以下先决条件,然后才能使用MongoDB Playground读取文档:
Create a connection to a MongoDB deployment.创建到MongoDB部署的连接。Activate the connection to the MongoDB deployment.激活到MongoDB部署的连接。Open a MongoDB Playground.打开一个MongoDB演练场。Create Documents 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. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. MongoDB for VS Code拆分您的演练场,并在Playground Results.json
窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for 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.在此文档顶部,单击“编辑文档”。MongoDB for VS Code opens it as an editable EJSON document titledMongoDB for 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 + S
to save the edited document to the MongoDB database.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会显示它。
Example实例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。
The following example:以下示例:
Switches to the切换到test
database.test
数据库。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, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的Playground,并在Playground Results.json
窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. 如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档。If you manually move your playground results, MongoDB for VS Code displays the results in that tab.如果手动移动演练场结果,MongoDB for 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. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code拆分您的演练场,并在Playground Results.json
窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出您的演练场的结果。
Example实例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。
The following example:以下示例:
Switches to the切换到test
database.test
数据库。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, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会拆分您的Playground,并在Playground Results.json
窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. 如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档。If you manually move your playground results, MongoDB for VS Code displays the results in that tab.如果手动移动演练场结果,MongoDB for VS Code会在该选项卡中显示结果。
[
{
_id: 2,
price: 10
},
{
_id: 6,
price: 10
},
{
_id: 9,
price: 10
},
{
_id: 1,
price: 10
}
]