NoSQL 数据库案例实战 --MongoDB 数据库 数据的插入、查询、更新、删除 ---- 强化练习(二)

MongoDB 数据库 数据的插入、查询、更新、删除 ---- 强化练习(二)

本环境是基于 Centos 7.8 系统构建mongodb-enterprise-4.2.8学习环境
具体构建,请参考MongoDB数据操作练习


1、创建一年级的3个班,并随机添加 10 名学生;

1

2

3

4

5

6

7

8

9

10

11

>for(grade_index in (grade = ['grade_1_1''grade_1_2''grade_1_3'])) {

       hobbys=['draw''dance''running''sing''football''basketball''computer''python']   

       for (var i = 1; i <= 10; i++) {

           db[grade[grade_index]].insert({

               "name""zhangsan" + i,

               "sex": Math.round(Math.random() * 10) % 2,

               "age": Math.round(Math.random() * 6) + 3,

               "hobby": [Hobbys[Math.round(Math.random() * 6)]]

           });

       }

   } 

查看

查看一年级二班grade_1_2中的所有学生

1

db.getCollection('grade_1_2').find({})

查看一年级二班grade_1_2中所有年龄是 4 岁的学生

1

db.getCollection('grade_1_2').find({ "age": 4})

查看一年级二班grade_1_2中所有年龄大于 4 岁的学生

1

db.getCollection('grade_1_2').find({ "age": {$gt: 4}})

查看一年级二班grade_1_2中所有年龄大于 4 岁并且小于 7 岁的学生

1

db.getCollection('grade_1_2').find({ "age": {$gt: 4, $lt: 7}})

查看一年级二班grade_1_2中所有年龄大于 4 岁并且性别值为0的学生

1

db.getCollection('grade_1_2').find({ "age": {$gt: 4}, "sex": 0})

查看一年级二班grade_1_2中所有年龄小于 4 岁或者大于 7 岁的学生

1

db.getCollection('grade_1_2').find({$or: [{ "age": {$lt: 4}}, { "age": {$gt: 6}}]})

查看一年级二班grade_1_2中所有年龄是 4 岁或 6 岁的学生

1

db.getCollection('grade_1_2').find({ "age": {$in: [4, 6]}})

查看一年级二班grade_1_2中所有姓名带zhangsan1的学生

1

db.getCollection('grade_1_2').find({ "name": {$regex: "zhangsan1"}})

查看一年级二班grade_1_2中所有姓名带zhangsan1和zhangsan2的学生

1

db.getCollection('grade_1_2').find({ "name": {$in: [new RegExp(""zhangsan1"), new RegExp(""zhangsan2")]}}) 

查看一年级二班grade_1_2中所有兴趣爱好有三项的学生

1

db.getCollection('grade_1_2').find({ "hobby": {$size: 3}}) 

查看一年级二班`grade_1_2`中所有兴趣爱好包括画画的学生

1

db.getCollection('grade_1_2').find({ "hobby""drawing"})

查看一年级二班`grade_1_2`中所有兴趣爱好既包括画画又包括跳舞的学生

1

db.getCollection('grade_1_2').find({ "hobby": {$all: ["drawing""dance"]}}) 

查看一年级二班grade_1_2中所有兴趣爱好有三项的学生的学生数目

1

db.getCollection('grade_1_2').find({ "hobby": {$size: 3}}).count() 

查看一年级二班的第二位学生

1

db.getCollection('grade_1_2').find({}).limit(1).skip(1) 

查看一年级二班的学生,按年纪升序

1

db.getCollection('grade_1_2').find({}).sort({ "age": 1}) 

查看一年级二班的学生,按年纪降序

1

db.getCollection('grade_1_2').find({}).sort({ "age": -1}) 

查看一年级二班的学生,年龄值有哪些

1

db.getCollection('grade_1_2').distinct('age')  

查看一年级二班的学生,兴趣覆盖范围有哪些

1

db.getCollection('grade_1_2').distinct('hobby')  

查看一年级二班的学生,男生(`sex`为 0)年龄值有哪些

1

db.getCollection('grade_1_2').distinct('age', { "sex": 0})

删除

一年级二班grade_1_2, 删除所有 4 岁的学生

1

db.getCollection('grade_1_2').remove({ "age": 4})  

一年级二班grade_1_2, 删除第一位 6 岁的学生

1

db.getCollection('grade_1_2').remove({ "age": 6}, {justOne: 1})  

修改

一年级二班grade_1_2中,修改名为zhangsan7的学生,年龄为 8 岁,兴趣爱好为 跳舞和画画;

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$set: { "age": 8, "hobby": ["dance""drawing"]}}) 

 一年级二班`grade_1_2`中,追加zhangsan7`学生兴趣爱好唱歌;

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$push: { "hobby""sing"}}) 

 一年级二班`grade_1_2`中,追加zhangsan7`学生兴趣爱好吹牛和打篮球;

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$push: { "hobby": {$each: ["brag""play_basketball"]}}}) 

 一年级二班`grade_1_2`中,追加`zhangsan7`学生兴趣爱好唱歌和打篮球,要保证`hobby`数组不重复;

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$addToSet: { "hobby": {$each: ["sing1""play_basketball"]}}})  

新学年,给一年级二班所有学生的年龄都增加一岁

1

db.getCollection('grade_1_2').update({}, {$inc: { "age": 1}}, {multi: true})  

一年级二班grade_1_2中,删除zhangsan7学生的sex属性

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$unset: { "sex": 1}}) 

一年级二班grade_1_2中,删除zhangsan7学生的hobby数组中的头元素

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$pop: { "hobby": -1}})  

 一年级二班`grade_1_2`中,删除`zhangsan7`学生的`hobby`数组中的尾元素

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$pop: { "hobby": 1}})  

 一年级二班`grade_1_2`中,删除`zhangsan7`学生的`hobby`数组中的`sing`元素

1

db.getCollection('grade_1_2').update({ "name""zhangsan7"}, {$pull: { "hobby""sing"}}  

分组

新建一个集合grade_1_4,记录一年级四班在期中考试时的成绩;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

for (var i = 1; i <= 10; i++) {

       db.grade_1_4.insert({

           "name""zhangsan" + i,

           "sex": Math.round(Math.random() * 10) % 2,

           "age": Math.round(Math.random() * 6) + 3,

           "score": {

               "chinese": 60 + Math.round(Math.random() * 40),

               "math": 60 + Math.round(Math.random() * 40),

               "english": 60 + Math.round(Math.random() * 40)

           }

       });

   }  

统计每名学生在考试中的总分 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

db.grade_1_4.group({

       key: { "name": 1},

       cond: {},

       reduce: function(curr, result) {

result.total += curr.score.chinese + curr.score.math + curr.score.english;

       },

       initial: { total : 0 }

   })

统计每名男生在考试中的总分

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  db.grade_1_4.group({

       key: { "name": 1},

       cond: { "sex": 0},

       reduce: function(curr, result) {

result.total += curr.score.chinese + curr.score.math + curr.score.english;

       },

       initial: { total : 0 }

   })  

统计每名男生在考试中的总分及平均分 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

db.grade_1_4.group({

       key: { "name": 1},

       cond: { "sex": 0},

       reduce: function(curr, result) {

result.total += curr.score.chinese + curr.score.math + curr.score.english;

       },

       initial: { total : 0 },

       finalize: function(item) {

item.avg = (item.total / 3).toFixed(2);

return item;

       }

   })  

聚合

根据姓名分组, 并统计人数 

1

db.getCollection('grade_1_4').aggregate([{$group: {_id: "$name", num: {$sum: 1}}}])  

根据姓名分组, 并统计人数,过滤人数大于 1 的学生

1

2

3

4

5

6

7

db.getCollection('grade_1_4').aggregate([

      {$group: {_id: "$name", num: {$sum: 1}}},

      {$match: {num: {$gt: 1}}}

  ])

统计每名学生在考试中的总分

1

2

3

4

5

   db.getCollection('grade_1_4').aggregate([

       {$group: {_id: "$name", score: {$sum: {$sum: ["$score.chinese""$score.math""$score.english"]}}}}

])

统计每名男生在考试中的总分

1

2

3

4

5

6

7

db.getCollection('grade_1_4').aggregate([

   {$match: {sex: 0}},

   {$group: {_id: "$name", score: {$sum: {$sum: ["$score.chinese""$score.math""$score.english"]}}}}

])

统计每名男生在考试中的总分, 总分降序

1

2

3

4

5

6

7

8

9

db.getCollection('grade_1_4').aggregate([

       {$match: {sex: 0}},

       {$group: {_id: "$name", score: {$sum: {$sum: ["$score.chinese""$score.math""$score.english"]}}}},

       {$sort: {score: 1}}

])

猜你喜欢

转载自blog.csdn.net/XY0918ZWQ/article/details/113827090