这个教程讲述了如何在 MongoDB 数据库中插入数据,并使用内嵌的 shell 查询数据
一、Switch Database
db
db
显示当前的数据库
use examples
use <db>
切换数据库
二、Populate a Collection (Insert)
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
}
])
- mongoDB 在 collections 中存储 documents
- collections 与关系型数据库中的 tables 很像
- 如果 collection 不存在,mongoDB 在你首次在 collection 存储数据的时候自动创建
db.collection.insertMany()
插入新 documents 到movies
collection- 上面命令执行完后,会返回一个 document,包含一个 acknowledgement indicator 以及一个数组,该数组包含每个成功插入的 documents 的 id
三、Find All Documents
db.movies.find({})
db.collection.find()
可以从一个 collection 选择全部的 documents
四、Filter Data with Comparision Operators
在查询过滤文档中指定<field>: <value>
db.movies.find( { "directors": "Christopher Nolan" } );
过滤查询电影的导演为 Christopher Nolan
db.movies.find( { "released": { $lt: ISODate("2000-01-01") } } );
过滤查询电影的发型日期在 2000 年之前
db.movies.find( { "awards.wins": { $gt: 100 } } );
过滤查询电影获奖超过 100 个
db.movies.find( { "languages": { $in: [ "Japanese", "Mandarin" ] } } )
过滤查询电影的语言有 Japanese 和 Mandarin
五、Specify Fields to Return (Projection)
db.movies.find( { }, { "title": 1, "directors": 1, "year": 1 } );
db.movies.find( { }, { "_id": 0, "title": 1, "genres": 1 } );
<field>: 1
返回的 documents 包含该字段<field>: 0
返回的 documents 不包含该字段_id
字段默认包含,其他字段默认不包含
六、Aggregate Data ($group)
db.movies.aggregate( [
{ $unwind: "$genres" },
{
$group: {
_id: "$genres",
genreCount: { $count: { } }
}
},
{ $sort: { "genreCount": -1 } }
] )