> [Getting Started — MongoDB Manual](https://www.mongodb.com/docs/manual/tutorial/getting-started/) > 版本 6.0 # 开始 ## 1. 切换数据库 - 查看当前数据库 ```bash $ db # 默认为test ``` - 切换创建数据库 - 切换数据库如果不存在就创建,但是不会显示,除非给里面插入至少一条数据 ```bash $ use databaseName ``` ## 2. 插入 - 在集合movies插入数据,会创建集合*movies* - `db.movies.insertMany()` ```bash 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 } ]) { acknowledged: true, insertedIds: { '0': ObjectId("64059011a1367911c5f03839"), '1': ObjectId("64059011a1367911c5f0383a"), '2': ObjectId("64059011a1367911c5f0383b"), '3': ObjectId("64059011a1367911c5f0383c") } } ``` ## 3. 查找全部 - 在选择的数据库中进行某个集合的查询`db.movies.find( { } )` ```bash $ db.expressgy.find() { _id: ObjectId("64055bb6d44cd6e5278cc4c8"), name: '何夕' } ``` ## 4. 过滤数据 - 条件查询 ### 4.1 属性相等 ```bash db.movies.find( { "directors": "Christopher Nolan" } ); { _id: ObjectId("64059011a1367911c5f0383a"), title: 'The Dark Knight', year: 2008, genres: ['Action', 'Crime', 'Drama'], rated: 'PG-13', languages: ['English', 'Mandarin'], released: 2008 - 07 - 18 T00: 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'] } ``` ### 4.2 小于 ```bash db.movies.find( { "released": { $lt: ISODate("2000-01-01") } } ); ``` ### 4.3 大于 ```bash db.movies.find( { "awards.wins": { $gt: 100 } } ); ``` ### 4.4 属于 ```bash db.movies.find( { "languages": { $in: [ "Japanese", "Mandarin" ] } } ) ``` ## 查询选择器 ### 比较 有关不同 BSON 类型值的比较,请参阅[指定 BSON 比较顺序。](https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order) `$eq` 匹配等于指定值的值。 `$gt` 匹配大于指定值的值。 `$gte` 匹配大于或等于指定值的值。 `$in` 匹配数组中指定的任何值。 `$lt` 匹配小于指定值的值。 `$lte` 匹配小于或等于指定值的值。 `$ne` 匹配所有不等于指定值的值。 `$nin` 与数组中指定的值均不匹配。 ### 逻辑 `$and` 将查询子句与逻辑返回与这两个子句的条件匹配的所有文档联接在一起。`AND` `$not` 反转查询表达式的效果,并返回与查询_表达式不匹配的文档_。 `$nor` 将查询子句与逻辑联接,返回与这两个子句不匹配的所有文档。`NOR` `$or` 将查询子句与逻辑返回与任一子句的条件匹配的所有文档联接在一起。`OR` ### 元素 `$exists` 匹配具有指定字段的文档。 `$type` 如果字段为指定类型,则选择文档。 ### 评估 `$expr` 允许在查询语言中使用聚合表达式。 `$jsonSchema` 根据给定的 JSON 架构验证文档。 `$mod` 对字段的值执行模运算,并选择具有指定结果的文档。 `$regex` 选择值与指定正则表达式匹配的文档。 `$text`执行文本搜索。 `$where` 匹配满足 JavaScript 表达式的文档。 ## 5. 项目字段 - 通过0,1控制查询的字段 ```bash db.movies.find( { }, { "title": 1, "directors": 1, "year": 1 } ); db.movies.find( { }, { "_id": 0, "title": 1, "genres": 1 } ); ```