mongodb作业

1.创建一个数据库 名字grade

powershell 复制代码
use grade

2.数据库中创建一个集合名字 class

powershell 复制代码
db.createCollection("class")

3.集合中插入若干数据

powershell 复制代码
db.class.insert([{name:"zhang",age:10,sex:'m',hobby:['a','b','c']}])

4.查找

查看班级所有人信息

powershell 复制代码
db.class.find()

查看班级中年龄为8岁的学生信息

powershell 复制代码
db.class.find({age:8})

查看年龄大于10岁的学生信息

powershell 复制代码
db.class.find({age:{$gt:10}})

查看年龄在 4---8岁之间的学生信息

powershell 复制代码
db.class.find({age:{$gt:4,$lte:8}})

找到年龄为6岁且为男生的学生

powershell 复制代码
db.class.find({age:6,sex:'m'})

找到年龄小于7岁或者大于10岁的学生

powershell 复制代码
db.class.find({$or:[age:{$lt:7},age:{$gt:10}]})

找到年龄是8岁或者11岁的学生

powershell 复制代码
db.class.find({age:{$in:[8,11]}})

找到兴趣爱好有两项的学生

powershell 复制代码
db.class.find({hobby:{$size:2}})

找到兴趣爱好有draw的学生

powershell 复制代码
db.class.find({hobby:"draw"})

找到既喜欢画画又喜欢跳舞的学生

powershell 复制代码
db.class.find({hobby:{$all:["draw","dance"]}})

统计爱好有三项的学生人数

powershell 复制代码
db.class.find({hobby:{$size:3}}).count()

找出本班年龄第二大的学生

powershell 复制代码
db.class.find().sort({age:-1}).skip(1).limit(1)

查看学生的兴趣范围

powershell 复制代码
db.class.distinct('hobby')

将学生按年龄排序找到年龄最大的三个

powershell 复制代码
db.class.find().sort({age:-1}).limit(3)

删除所有 年级大于12或者小于4岁的学生

powershell 复制代码
remove({$or:[{age:{$gt:12}},{age:{$lt:4}}]})

5.增加、更新、删除、统计

将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画

powershell 复制代码
{$set:{age:8,hobby:['dance','draw']}}

追加小明兴趣爱好 唱歌

powershell 复制代码
{$push:{hobby:'sing'}}

小王兴趣爱好增加 吹牛 打篮球

powershell 复制代码
{$pushAll:{hobby:['吹牛','basketball']}}

小李增加爱好,跑步和唱歌,但是不要和以前的重复

powershell 复制代码
{$addToSet:{hobby:{$each:['running','sing']}}}

该班所有同学年龄加1

powershell 复制代码
update({},{$inc:{age:1}},false,true)

删除小明的sex属性

powershell 复制代码
{$unset:{sex:0}}

删除小李兴趣中的第一项

powershell 复制代码
{$pop:{hobby:-1}}

将小红兴趣中的画画爱好删除

powershell 复制代码
{$pull:{hobby:'draw'}}

增加分数域 score:{'chinese':88,'english':78,'math':98}

按照性别分组统计每组人数

powershell 复制代码
 aggregate({$group:{_id:'$sex',num:{$sum:1}}})

按照姓名分组,过滤出有重名的同学

powershell 复制代码
aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])

统计每名男生的语文成绩

powershell 复制代码
aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])

将女生按照英语分数降序排列

powershell 复制代码
aggregate([{$match:{sex:'w'}},{$sort:{'score.english':-1}}])
相关推荐
青梅橘子皮4 小时前
Linux---基本指令
linux·运维·服务器
REDcker4 小时前
Linux信号机制详解 POSIX语义与内核要点 sigaction与备用栈实践
linux·运维·php
cui_ruicheng5 小时前
Linux进程间通信(三):System V IPC与共享内存
linux·运维·服务器
蚰蜒螟5 小时前
深入 Linux 内核同步机制:从 futex 到 spinlock 的完整旅程
linux·windows·microsoft
运维全栈笔记5 小时前
Linux安装配置Tomcat保姆级教程:从部署到性能调优
linux·服务器·中间件·tomcat·apache·web
dllmayday6 小时前
Linux 上用终端连接 WiFi
linux·服务器·windows
峥无8 小时前
Linux系统编程基石:静态库·动态库·ELF文件·进程地址空间全景图
linux·运维·服务器
用户2367829801688 小时前
从 chmod 755 说起:Unix 文件权限到底是怎么算的?
linux
Strugglingler8 小时前
【systemctl 学习总结】
linux·systemd·systemctl·journalctl·unit file
嵌入式×边缘AI:打怪升级日志9 小时前
100ASK-T113 Pro 开发板 Bootloader 完全开发指南
linux·ubuntu·bootloader