Getting Started起步

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驱动程序的示例,请参阅附加示例部分中的链接。

Examples示例

Click inside the shell to connect.在shell内部单击以连接。Once connected, you can run the examples in the shell above.连接后,可以运行上面shell中的示例。

Within the shell, db refers to your current database.shell中,db引用当前数据库。Type db to display the current database.键入db以显示当前数据库。

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数据库:

use 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

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中。

db.inventory.insertMany([
   { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank" ] },
   { item: "paper", qty: 10, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: [ "red", "blank", "plain" ] },
   { item: "planner", qty: 0, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: [ "blank", "red" ] },
   { item: "postcard", qty: 45, status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: [ "blue" ] }
]);

// MongoDB adds an _id field with an ObjectId value if the field is not present in the document

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 inventory collection.shell中,复制并粘贴以下内容以返回库存集合中的所有文档。

db.inventory.find({})

To format the results, append the .pretty() to the find operation:要格式化结果,请将.pretty()追加到find操作后面:

db.inventory.find({}).pretty()

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集合。

  • In the shell, copy and paste the following to return documents where status field equals "D":shell中,复制并粘贴以下内容以返回status字段为"D"的文档:

    db.inventory.find( { status: "D" } );
  • In the shell, copy and paste the following to return document where qty field equals 0:shell中,复制并粘贴以下内容以返回其qty字段等于0的文档:

    db.inventory.find( { qty: 0 } );
  • In the shell, copy and paste the following to return document where qty field equals 0 and status field equals "D":shell中,复制并粘贴以下内容以返回其qty字段等于0并且status字段等于"D"的文档:

    db.inventory.find( { qty: 0, status: "D" } );
  • In the shell, copy and paste the following to return document where the uom field, nested inside the size document, equals "in":shell中,复制并粘贴以下内容以返回其嵌套在size文档中的uom字段等于"in"的文档:

    db.inventory.find( { "size.uom": "in" } )
  • In the shell, copy and paste the following to return document where the size field equals the document { h: 14, w: 21, uom: "cm" }:shell,复制并粘贴以下内容, 返回其size字段等于文档{ h:14, w:21, uom:"cm" }的文档:

    db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )

    Equality matches on the embedded document require an exact match, including the field order.嵌入文档上的相等匹配要求完全匹配,包括字段顺序。

  • In the shell, copy and paste the following to return documents where the tags array contains "red" as one of its elements:shell中,复制并粘贴以下内容以返回tags数组中包含"red"作为其元素之一的文档:

    db.inventory.find( { tags: "red" } )

    If the tags field is a string instead of an array, then the query is just an equality match.如果tags字段是字符串而不是数组,那么该查询就是一个相等匹配。

  • In the shell, copy and paste the following to return documents where the tags field matches the specified array exactly, including the order:shell中,复制并粘贴以下内容以返回其tags字段严格匹配指定数组的文档(包括匹配顺序):

    db.inventory.find( { tags: [ "red", "blank" ] } )

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 to include a field in the returned documents在返回的文档中包含字段
  • <field>: 0 to exclude a field in the returned documents在返回的文档中排除字段

In the shell, copy and paste the following to return the _id, item, and the status fields from all documents in the inventory collection:shell中,复制并粘贴以下内容以从inventory集合中的所有文档中返回_iditemstatus字段:

db.inventory.find( { }, { item: 1, status: 1 } );

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.若要排除该字段,请在投影文档中把它设置为0For example, copy and paste the following to return only the item, and the status fields in the matching documents:例如,复制并粘贴以下内容以只返回匹配文档中的item字段和status字段:

db.inventory.find( {}, { _id: 0, item: 1, status: 1 } );

Next Steps下一步

Set up Your Own Deployment设置自己的部署

To set up your own deployment:要设置自己的部署,请执行以下操作:

MongoDB Atlas Free Tier ClusterMongoDB Atlas免费层集群 MongoDB Atlas is a fast, easy, and free way to get started with MongoDB.MongoDB Atlas是一种快速、简单、免费的方式来开始使用MongoDB。To learn more, see the Getting Started with Atlas tutorial.要了解更多信息,请参阅Atlas入门教程
Local MongoDB installation本地MongoDB安装 For more information on installing MongoDB locally, see Install MongoDB.有关在本地安装MongoDB的更多信息,请参阅安装MongoDB

Additional Examples其他示例

For additional examples, including MongoDB driver specific examples (Python, Java, Node.js, etc.), see:对于其他示例,包括MongoDB驱动程序特定的示例(Python、Java、Node.js等),参见:

Query document examples
Update document examples更新文档示例
Delete document examples删除文档示例