1.进入mongodb安装目录(>cd D:\workPrograms\mongodb\bin)

2.>mongodb   启动mongo db

3.>show dbs;   查询所有数据库

4.>use test;   选择要查询的数据库,此处若test数据库不存在会自动创建test数据库

5.在users collection中插入6条用户数据

>db.users.insertMany(

  [
     {
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites: { artist: "Picasso", food: "pizza" },
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 85, bonus: 10 }
       ]
     },
     {
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites: { artist: "Miro", food: "meringue" },
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 64, bonus: 12 }
       ]
     },
     {
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 }
       ]
     },
     {
       _id: 4,
       name: "xi",
       age: 34,
       type: 2,
       status: "D",
       favorites: { artist: "Chagall", food: "chocolate" },
       finished: [ 5, 11 ],
       badges: [ "red", "black" ],
       points: [
          { points: 53, bonus: 15 },
          { points: 51, bonus: 15 }
       ]
     },
     {
       _id: 5,
       name: "xyz",
       age: 23,
       type: 2,
       status: "D",
       favorites: { artist: "Noguchi", food: "nougat" },
       finished: [ 14, 6 ],
       badges: [ "orange" ],
       points: [
          { points: 71, bonus: 20 }
       ]
     },
     {
       _id: 6,
       name: "abc",
       age: 43,
       type: 1,
       status: "A",
       favorites: { food: "pizza", artist: "Picasso" },
       finished: [ 18, 12 ],
       badges: [ "black", "blue" ],
       points: [
          { points: 78, bonus: 8 },
          { points: 57, bonus: 7 }
       ]
     }
  ]
)

6.>db.users.find(); 查询users里的所有数据

7.>db.users.find({status: 'A'}); 查询users里status等于A的数据

8.>db.users.find({age:{$gt:22}}); 查询users里年龄大于22的数据

9.> db.users.find({status: 'D', age: {$gt: 23}}); 查询users里状态为D并且年龄大于23的数据

10.>db.users.find({$or: [{status: 'A'}, {age: {$gt: 23}}]}); 查询users里状态为A或者年龄大于23的数据

11.>db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } ); 嵌套查询,匹配整个子文档,查询favorites里artist为Picasso,food为pizza的用户

12.>db.users.find( { "favorites.artist": "Picasso" } );  嵌套查询,匹配子文档中的字段,通过点(.)符号来表示子文档中的字段

13.>db.users.find( { badges: [ "blue", "black" ] } ); 数组查询,匹配所有数组,查找badges的值为"['blue', 'black']"的用户

14.>db.users.find( { badges: "black" } ); 数组查询,查询数组中的一个元素,查找badges的数组中包含了'black'元素的所有用户

15.> db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } ); 数组多条件and查询($elemMatch),$elemMatch多个条件and匹配,假设我们要找到满足finished字段中的数组元素的值大于15并且小于20

      结果:{ "_id" : 1, ..., "finished" : [ 17, 3 ] ,...}

     { "_id" : 6, ..., "finished" : [ 18, 12 ], ...}

16.>db.users.find( { finished: { $gt: 15, $lt: 20 } } );  数组多条件or查询,查询大于15或者小于20的数据

    结果:{ "_id" : 1,..., "finished" : [ 17, 3 ], ...}

    { "_id" : 2, ..., "finished" : [ 11, 25 ], ...}
    { "_id" : 6, ..., "finished" : [ 18, 12 ], ...}

17.>db.users.find( { 'points.0.points': { $lte: 55 } } ); 使用数组下标定位集合子文档,查询数组中第一个points的值,小于等于55的数据

18.>db.users.find( { 'points.points': { $lte: 55 } } ); 省略下标,查询points数组中所有的points的值小于等于55的数据

19.> db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } ); 多条件and查询($elemMatch),查询points数组中points的值小于等于70并且bonus为20的数据

     结果:{ "_id" : 3, ....,"points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }

20.>db.users.find({"points.points": {$lte: 70}, "points.bonus": 20}); 多条件or查询, 查询points数组中,points的值小于等于70或者bonus为20的数据

      结果:{ "_id" : 2, ..., "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }

    { "_id" : 3, ..., "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐