这个教程讲述了如何在 MongoDB 数据库中插入数据,并使用内嵌的 shell 查询数据

一、Switch Database

  1. db
  • db显示当前的数据库
  1. use examples
  • use <db>切换数据库

二、Populate a Collection (Insert)

  1. db.movies.insertMany([
  2. {
  3. title: 'Titanic',
  4. year: 1997,
  5. genres: [ 'Drama', 'Romance' ],
  6. rated: 'PG-13',
  7. languages: [ 'English', 'French', 'German', 'Swedish', 'Italian', 'Russian' ],
  8. released: ISODate("1997-12-19T00:00:00.000Z"),
  9. awards: {
  10. wins: 127,
  11. nominations: 63,
  12. text: 'Won 11 Oscars. Another 116 wins & 63 nominations.'
  13. },
  14. cast: [ 'Leonardo DiCaprio', 'Kate Winslet', 'Billy Zane', 'Kathy Bates' ],
  15. directors: [ 'James Cameron' ]
  16. },
  17. {
  18. title: 'The Dark Knight',
  19. year: 2008,
  20. genres: [ 'Action', 'Crime', 'Drama' ],
  21. rated: 'PG-13',
  22. languages: [ 'English', 'Mandarin' ],
  23. released: ISODate("2008-07-18T00:00:00.000Z"),
  24. awards: {
  25. wins: 144,
  26. nominations: 106,
  27. text: 'Won 2 Oscars. Another 142 wins & 106 nominations.'
  28. },
  29. cast: [ 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart', 'Michael Caine' ],
  30. directors: [ 'Christopher Nolan' ]
  31. },
  32. {
  33. title: 'Spirited Away',
  34. year: 2001,
  35. genres: [ 'Animation', 'Adventure', 'Family' ],
  36. rated: 'PG',
  37. languages: [ 'Japanese' ],
  38. released: ISODate("2003-03-28T00:00:00.000Z"),
  39. awards: {
  40. wins: 52,
  41. nominations: 22,
  42. text: 'Won 1 Oscar. Another 51 wins & 22 nominations.'
  43. },
  44. cast: [ 'Rumi Hiiragi', 'Miyu Irino', 'Mari Natsuki', 'Takashi Naitè' ],
  45. directors: [ 'Hayao Miyazaki' ]
  46. },
  47. {
  48. title: 'Casablanca',
  49. genres: [ 'Drama', 'Romance', 'War' ],
  50. rated: 'PG',
  51. cast: [ 'Humphrey Bogart', 'Ingrid Bergman', 'Paul Henreid', 'Claude Rains' ],
  52. languages: [ 'English', 'French', 'German', 'Italian' ],
  53. released: ISODate("1943-01-23T00:00:00.000Z"),
  54. directors: [ 'Michael Curtiz' ],
  55. awards: {
  56. wins: 9,
  57. nominations: 6,
  58. text: 'Won 3 Oscars. Another 6 wins & 6 nominations.'
  59. },
  60. lastupdated: '2015-09-04 00:22:54.600000000',
  61. year: 1942
  62. }
  63. ])
  • mongoDB 在 collections 中存储 documents
  • collections 与关系型数据库中的 tables 很像
  • 如果 collection 不存在,mongoDB 在你首次在 collection 存储数据的时候自动创建
  • db.collection.insertMany()插入新 documents 到 moviescollection
  • 上面命令执行完后,会返回一个 document,包含一个 acknowledgement indicator 以及一个数组,该数组包含每个成功插入的 documents 的 id

三、Find All Documents

  1. db.movies.find({})
  • db.collection.find()可以从一个 collection 选择全部的 documents

四、Filter Data with Comparision Operators

在查询过滤文档中指定<field>: <value>

  1. db.movies.find( { "directors": "Christopher Nolan" } );
  • 过滤查询电影的导演为 Christopher Nolan

    1. db.movies.find( { "released": { $lt: ISODate("2000-01-01") } } );
  • 过滤查询电影的发型日期在 2000 年之前

    1. db.movies.find( { "awards.wins": { $gt: 100 } } );
  • 过滤查询电影获奖超过 100 个

    1. db.movies.find( { "languages": { $in: [ "Japanese", "Mandarin" ] } } )
  • 过滤查询电影的语言有 Japanese 和 Mandarin

五、Specify Fields to Return (Projection)

  1. db.movies.find( { }, { "title": 1, "directors": 1, "year": 1 } );
  2. db.movies.find( { }, { "_id": 0, "title": 1, "genres": 1 } );
  • <field>: 1返回的 documents 包含该字段
  • <field>: 0返回的 documents 不包含该字段
  • _id字段默认包含,其他字段默认不包含

六、Aggregate Data ($group)

  1. db.movies.aggregate( [
  2. { $unwind: "$genres" },
  3. {
  4. $group: {
  5. _id: "$genres",
  6. genreCount: { $count: { } }
  7. }
  8. },
  9. { $sort: { "genreCount": -1 } }
  10. ] )

七、参考文档