The following page provides various examples for querying in the MongoDB shell.下面的页面提供了在MongoDB shell中进行查询的各种示例。For examples using MongoDB drivers, refer to the links in the Additional Examples section.有关使用MongoDB驱动程序的示例,请参阅附加示例部分中的链接。
Click inside the shell to connect.在shell内部单击以连接。Once connected, you can run the examples in the shell above.连接后,可以运行上面shell中的示例。
Within the shell, 在shell中,db
refers to your current database.db
引用当前数据库。Type 键入db
to display the current database.db
以显示当前数据库。
The operation should return 操作应返回test
, which is the default database.test
,这是默认数据库。
To switch databases, type 要切换数据库,请键入use <db>
.use<db>
。For example, to switch to the 例如,要切换到examples
database:examples
数据库:
You do not need to create the database before you switch.切换前不需要创建数据库。MongoDB creates the database when you first store data in that database (such as create the first collection in the database).MongoDB在您第一次将数据存储在数据库中时创建数据库(例如在数据库中创建第一个集合)。
To verify that your database is now 要验证您的数据库现在是examples
, type db
in the shell above.examples
,请在上面的shell中键入db
。
To create a collection in the database, see the next tab.要在数据库中创建集合,请参见下一个选项卡。
MongoDB stores documents in collections.MongoDB将文档存储在集合中。Collections are analogous to tables in relational databases.集合类似于关系数据库中的表。If a collection does not exist, MongoDB creates the collection when you first store data for that collection.如果集合不存在,MongoDB将在您首次存储该集合的数据时创建该集合。
The following example uses the 下面的示例使用db.collection.insertMany()
method to insert new documents into the inventory
collection.db.collection.insertMany()
方法将新文档插入invertory
集合。You can copy and paste the example into the shell above.您可以将示例复制并粘贴到上面的shell中。
The operation returns a document that contains the acknowledgement indicator and an array that contains the 该操作返回一个文档,其中包含确认指示符和一个数组,该数组包含每个成功插入的文档的_id
of each successfully inserted documents._id
。
To verify the insert, you can query the collection (See the next tab).要验证插入,可以查询集合(请参见下一个选项卡)。
To select the documents from a collection, you can use the 要从集合中选择文档,可以使用db.collection.find()
method.db.collection.find()
方法。To select all documents in the collection, pass an empty document as the query filter document to the method.要选择集合中的所有文档,请将空文档作为查询筛选器文档传递给方法。
In the shell, copy and paste the following to return all documents in the 在shell中,复制并粘贴以下内容以返回库存集合中的所有文档。inventory
collection.
To format the results, append the 要格式化结果,请将.pretty()
to the find
operation:.pretty()
追加到find
操作后面:
Note
The example assumes that you have populated the 该示例假设您已从上一步填充了inventory
collection from the previous step.inventory
集合。
For an equality match (i.e. 对于相等匹配(即<field>
equals <value>
), specify <field>: <value>
in the query filter document and pass to the db.collection.find()
method.<field>
等于<value>
),请在查询筛选器文档中指定<field>: <value>
,并传递给db.collection.find()
方法。
Note
The examples assume that you have populated the 这些示例假设您已经填充了inventory
collection.inventory
集合。
status
field equals "D"
:status
字段为"D"
的文档:
qty
field equals 0
:qty
字段等于0
的文档:
qty
field equals 0
and status
field equals "D"
:qty
字段等于0
并且status
字段等于"D"
的文档:
uom
field, nested inside the size document, equals "in"
:size
文档中的uom
字段等于"in"
的文档:
size
field equals the document { h: 14, w: 21, uom: "cm" }
:size
字段等于文档{ h:14, w:21, uom:"cm" }
的文档:
Equality matches on the embedded document require an exact match, including the field order.嵌入文档上的相等匹配要求完全匹配,包括字段顺序。
tags
array contains "red"
as one of its elements:tags
数组中包含"red"
作为其元素之一的文档:
If the 如果tags
field is a string instead of an array, then the query is just an equality match.tags
字段是字符串而不是数组,那么该查询就是一个相等匹配。
tags
field matches the specified array exactly, including the order:tags
字段严格匹配指定数组的文档(包括匹配顺序):
To specify fields to return, pass a projection document to the 若要指定要返回的字段,就要向db.collection.find(<query document>, <projection document>)
method.db.collection.find(<query document>, <projection document>)
方法传递投影文档。In the projection document, specify:在投影文档中,指定:
<field>: 1
<field>: 0
In the shell, copy and paste the following to return the 在shell中,复制并粘贴以下内容以从_id
, item
, and the status
fields from all documents in the inventory
collection:inventory
集合中的所有文档中返回_id
、item
和status
字段:
You do not have to specify the 你不需要指定_id
field to return the field._id
字段以返回该字段。It returns by default.它是默认返回的。To exclude the field, set it to 若要排除该字段,请在投影文档中把它设置为0
in the projection document.0
。For example, copy and paste the following to return only the 例如,复制并粘贴以下内容以只返回匹配文档中的item
, and the status
fields in the matching documents:item
字段和status
字段:
To set up your own deployment:要设置自己的部署,请执行以下操作:
For additional examples, including MongoDB driver specific examples (Python, Java, Node.js, etc.), see:对于其他示例,包括MongoDB驱动程序特定的示例(Python、Java、Node.js等),参见:
Query document examples | |
|