本教程将引导您将测试数据导入 MongoDB 数据库,并使用文档的嵌入式 Web shell 查询该数据。您无需部署或安装 MongoDB 即可完成本教程。
本教程中的示例中导入的是 Mflix 数据集的子集 ,该子集是 MongoDB 的云托管服务MongoDB Atlas中包含的示例数据的一部分。Atlas 不需要安装开销,并提供免费套餐公开使用。
完成本教程后,您可以使用 Atlas 导入其他示例数据或使用您自己的数据。

在 Shell 中进行连接。
连接完成以后,您可以在上面的 shell 中运行示例。
在shell中,是指您当前的数据库。键入 db 显示当前数据库:
dbdb
该操作应返回 ,这是默认数据库。test
要切换数据库,请键入下述语句。
例如,要切换到数据库:use <db>examples
在切换之前,您不需要创建数据库。MongoDB在您第一次将数据导入在该数据库中时创建数据库(例如在数据库中创建第一个集合)。
要验证您的数据库现在是否为当前数据库,请键入examples db
MongoDB将文档存储在集合中,集合类似于关系数据库中的表。如果集合不存在,MongoDB 会在您首次存储该集合的数据时创建该集合。
下面的示例使用db.collection.insertMany()方法将新文档插入到集合中。
db.movies.insertMany([
{
title: 'Titanic',
year: 1997,
genres: [ 'Drama', 'Romance' ],
rated: 'PG-13',
languages: [ 'English', 'French', 'German', 'Swedish', 'Italian', 'Russian' ],
released: ISODate("1997-12-19T00:00:00.000Z"),
awards: {
wins: 127,
nominations: 63,
text: 'Won 11 Oscars. Another 116 wins & 63 nominations.'
},
cast: [ 'Leonardo DiCaprio', 'Kate Winslet', 'Billy Zane', 'Kathy Bates' ],
directors: [ 'James Cameron' ]
},
{
title: 'The Dark Knight',
year: 2008,
genres: [ 'Action', 'Crime', 'Drama' ],
rated: 'PG-13',
languages: [ 'English', 'Mandarin' ],
released: ISODate("2008-07-18T00:00:00.000Z"),
awards: {
wins: 144,
nominations: 106,
text: 'Won 2 Oscars. Another 142 wins & 106 nominations.'
},
cast: [ 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart', 'Michael Caine' ],
directors: [ 'Christopher Nolan' ]
},
{
title: 'Spirited Away',
year: 2001,
genres: [ 'Animation', 'Adventure', 'Family' ],
rated: 'PG',
languages: [ 'Japanese' ],
released: ISODate("2003-03-28T00:00:00.000Z"),
awards: {
wins: 52,
nominations: 22,
text: 'Won 1 Oscar. Another 51 wins & 22 nominations.'
},
cast: [ 'Rumi Hiiragi', 'Miyu Irino', 'Mari Natsuki', 'Takashi Naitè' ],
directors: [ 'Hayao Miyazaki' ]
},
{
title: 'Casablanca',
genres: [ 'Drama', 'Romance', 'War' ],
rated: 'PG',
cast: [ 'Humphrey Bogart', 'Ingrid Bergman', 'Paul Henreid', 'Claude Rains' ],
languages: [ 'English', 'French', 'German', 'Italian' ],
released: ISODate("1943-01-23T00:00:00.000Z"),
directors: [ 'Michael Curtiz' ],
awards: {
wins: 9,
nominations: 6,
text: 'Won 3 Oscars. Another 6 wins & 6 nominations.'
},
lastupdated: '2015-09-04 00:22:54.600000000',
year: 1942
}
])
该操作将返回一个文档,其中包含确认指示器和一个数组,该数组包含每个成功插入的文档。
若要从集合中选择文档,可以使用db.collection.find()方法。若要选择集合中的所有文档,请将空文档作为查询筛选器文档传递给该方法。
在shell中,复制并粘贴以下内容以返回集合中的所有文档。
db.movies.find( { } )
对于相等匹配 ( equals),请在查询筛选器文档中指定并传递给db.collection.find()方法。<field><value><field>: <value>
Christopher Nolandb.movies.find( { "directors": "Christopher Nolan" } );
您可以使用比较运算符来执行更高级的查询:
db.movies.find( { "awards.wins": { $gt: 100 } } );
languagesJapaneseMandarindb.movies.find( { "languages": { $in: [ "Japanese", "Mandarin" ] } } )
若要指定要返回的字段,请将投影文档传递给db.collection.find(<query document><projection document>)方法。在投影文档中,指定:
<field>: 1在返回的文档中包括字段<field>: 0以排除返回文档中的字段在shell中,运行以下查询以从集合中的所有文档中返回字段:idtitledirectorsyearmovies
db.movies.find( { }, { "title": 1, "directors": 1, "year": 1 } );
您不必指定字段即可返回该字段。默认情况下,它将返回。要排除该字段,请在投影文档中将其设置为。例如,运行以下查询以仅返回 匹配文档中的 和 字段:_id0titlegenres
db.movies.find( { }, { "_id": 0, "title": 1, "genres": 1 } );
$group)可以使用聚合将多个文档中的值组合在一起并返回单个结果。MongoDB中的聚合是使用聚合管道执行的。
虽然find()操作对于数据检索很有用,但聚合管道允许您操作数据、执行计算和编写比简单CRUD 操作更具表现力的查询。
在shell中,运行以下聚合管道来计算每个值的出现次数:genre
db.movies.aggregate( [
{ $unwind: "$genres" },
{
$group: {
_id: "$genres",
genreCount: { $count: { } }
}
},
{ $sort: { "genreCount": -1 } }
] )
该管道使用:
$unwind为数组中的每个元素输出一个文档。genres$group和$count累加器来计算每个.此值存储在字段中。genregenreCount$sort按字段降序对生成的文档进行排序。genreCount