mongdb练习题

第一大题:

1.清空并创建school数据库,并查看有哪些集合

use school;
db.dropDatabase();
use school;
show tables;

2.创建三年级二班(class_3_2), 三年级五班(class_3_5), 三年级八班(class_3_8)共3个班,

并给每个班随机添加 10 名学生, 包含学生的姓名(s_name), 学生的性别(s_sex), 学生的年龄(s_sex),学生的爱好(s_habit)

   for(class_index in (classes ='class_3_2', 'class_3_5', 'class_3_8'])) {
       for (var i = 1; i <= 10; i++) {
           db[classes[class_index]].insert({
               "s_name": "zhoujielun" + i,
               "s_sex": Math.round(Math.random() * 10) % 2,
               "s_age": Math.round(Math.random() * 4) + 10,
               "s_habit": ["dance", "eat"]
           });
       }
   }
mongorestore -d school --dir D:\mongodb\output\school -u okk --authenticationDatabase admin

-d 指恢复后的数据库
--dir 指数据来源
-u 用户名
--authenticationDatabase admin 授予权限

3.查看三年级二班中的所有学生

db.getCollection('class_3_2').find()    ---下面只用这个方式写
或
db.class_3_2.find()

4.查看三年级二班中所有年龄是 11岁的学生

db.getCollection('class_3_2').find({"s_age": 11})

5查看三年级二班中所有年龄大于 12 岁的学生

db.getCollection('class_3_2').find({"s_age": {$gt: 12}})

6查看三年级二班中所有年龄大于 12岁并且小于 14岁的学生

db.getCollection('class_3_2').find({"s_age": {$gt: 12, $lt: 14}})

7查看三年级二班中所有年龄大于 12 岁并且性别值为0的学生

db.getCollection('class_3_2').find({"s_age": {$gt: 12}, "s_sex": 0})

8查看三年级二班中所有年龄小于 12 岁并且大于13岁的学生

db.getCollection('class_3_2').find({$or: [{"s_age": {$lt: 12}}, {"s_age": {$gt: 13}}]})

9查看三年级二班中所有年龄是11 岁或 13 岁的学生

db.getCollection('class_3_2').find({"s_age": {$in: [11, 13]}})

10.查看三年级二班中所有姓名带zhoujielun1字符的学生

db.getCollection('class_3_2').find({"s_name": {$regex: "zhoujielun1"}})

11.查看三年级二班中所有姓名带zhoujielun1zhoujielun2字符的学生

db.getCollection('class_3_2').find({"s_name": {
   $in: [new RegExp("zhoujielun1"), new RegExp("zhoujielun2")]
}})

12.三年二班,修改名为zhoujielun7的学生,年龄为9 岁,兴趣爱好为 跳舞和画画;

db.getCollection('class_3_2').update({"s_name": "zhoujielun7"}, {$set: {"s_age": 8, "s_habit": ["dance", "drawing"]}})
  1. 三年二班中,追加zhoujielun7学生兴趣爱好唱歌;
<
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值