关于你欠缺的NoSQL中的redis和mongoDB

news2024/11/24 8:46:11

文章目录

  • 前言
  • 一、在string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和hash还需要增加遍历的操作命令
    • 1、STRING类型
    • 2、List类型数据的命令操作:
    • 3、举例说明list和hash的应用场景,每个至少一个场景
  • 二、MongoDB作业
    • 1.准备工作
    • 2. 查找
    • 增加、更新、删除、统计
    • 3、增加分数域 score:{'chinese':88,'english':78,'math':98}


前言

不知不觉已经进入NoSQL的学习来了,那么什么是NoSQL呢?NoSQL的数据分类有哪一些呢?
一、什么是NoSQL呢?

NoSQL,指的是非关系型的数据库。NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系
型数据库的数据库管理系统的统称。
NoSQL用于超大规模数据的存储。(例如谷歌或Facebook每天为他们的用户收集万亿比特的数据)。这
些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。

二、NoSQL的数据分类有哪一些呢?

1、键值数据库
相关产品:Redis、Riak、SimpleDB、Chordless、Scalaris、Memcached
应用:内容缓存
优点:扩展性好、灵活性好、大量写操作时性能高
缺点:无法存储结构化信息、条件查询效率较低
使用者:百度云(Redis)、GitHub(Riak)、BestBuy(Riak)、Twitter(Ridis和
Memcached)
2、列族数据库
相关产品:BigTable、HBase、Cassandra、HadoopDB、GreenPlum、PNUTS
应用:分布式数据存储与管理
优点:查找速度快、可扩展性强、容易进行分布式扩展、复杂性低
使用者:Ebay(Cassandra)、Instagram(Cassandra)、NASA(Cassandra)、
Facebook(HBase)
3、文档数据库
相关产品:MongoDB、CouchDB、ThruDB、CloudKit、Perservere、Jackrabbit
应用:存储、索引并管理面向文档的数据或者类似的半结构化数据
优点:性能好、灵活性高、复杂性低、数据结构灵活
缺点:缺乏统一的查询语言
使用者:百度云数据库(MongoDB)、SAP(MongoDB)
4、图形数据库
相关产品:Neo4J、OrientDB、InfoGrid、GraphDB
应用:大量复杂、互连接、低结构化的图结构场合,如社交网络、推荐系统等
优点:灵活性高、支持复杂的图形算法、可用于构建复杂的关系图谱
缺点:复杂性高、只能支持一定的数据规模
使用者:Adobe(Neo4J)、Cisco(Neo4J)、T-Mobile(Neo4J)

一、在string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和hash还需要增加遍历的操作命令

REDIS中一些常用的命令
LPUSH/LPUSHX:LPUSH是将值插入到链表的头部,LPUSHX是检测这个链表是否存在,如果存在的话会插入头部,如果不存在会忽略这个数据
RPUSH/RPUSHX:将值插入到链表的尾部。同上,位置相反
LPOP:移除并获取链表中的第一个元素。
RPOP:移除并获取链表中最后一个元素。
LTRIM:保留指定区间内的元素。
LLEN:获取链表的长度。
LSET:用索引设置链表元素的值。
LINDEX:通过索引获取链表中的元素。
LRANGE:获取链表指定范围内的元素。

1、STRING类型

(1) 设置键值:SET用于添加键值对,值 为string类型

①127.0.0.1:6379> set key value EX 100
OK
SET key value EX 秒数 / PX 毫秒数 NX/XX

②127.0.0.1:6379> SETNX key value
0

127.0.0.1:6379> SETNX key1 value
1
SETNX key value
命令在设置成功时返回 1   设置失败时返回 0 

③127.0.0.1:6379> SETEX key2 100 value
OK
SETEX key 秒数 value

④127.0.0.1:6379> PSETEX KEY3 100 VALUE
OK
PSETEX key 毫秒数 value

⑤127.0.0.1:6379> TTL KEY4
-2
⑥127.0.0.1:6379> PTTL KEY4
-2


(2) 读取键值:GET KEY_NAME

127.0.0.1:6379> SETEX KEYA 10 "HELLO CHINA"
OK
127.0.0.1:6379> GET KEYA
HELLO CHINA
、
当我们再去get键值的时候,却发现什么都没有,因为这是设置的时间为10s,这个时候已经过期了。
127.0.0.1:6379> GET KEYA

(3) 数值类型自增1:INCR KEY_NAME

127.0.0.1:6379> set mykey 20
OK
127.0.0.1:6379> incr mykey
21

(4) 数值类型自减1:DECR KEY_NAME


127.0.0.1:6379> set mykey 20
OK
127.0.0.1:6379> decr mykey
19

(5) 查看值的长度:SETRLEN KEY_NAME

127.0.0.1:6379> set key "this is my country china"
OK
127.0.0.1:6379> strlen key
24

2、List类型数据的命令操作:

(1)对列表city插入元素:Shanghai Suzhou Hangzhou

127.0.0.1:6379> lrange city 0 8
Hangzhou
Suzhou
Shanghai
tianjing
xian
chengdu
Chongqing

(2)将列表city里的头部的元素移除

127.0.0.1:6379> lpop city
Hangzhou
127.0.0.1:6379> lrange city 0 10
Suzhou
Shanghai
tianjing
xian
chengdu
chongqing
127.0.0.1:6379> 

(3)将name列表的尾部元素移除到number列表的头部
相关代码:RPOPLPUSH source destination

127.0.0.1:6379> lrange name 0 6
Mikky
Coco
Simmons
Tom
Susan
127.0.0.1:6379> rpoplpush name number
Susan
127.0.0.1:6379> lrange name 0 -1
Mikky
Coco
Simmons
Tom
127.0.0.1:6379> lrange number 0 -1
Susan
5
4
3
2
1

(4) 对一个已存在的列表插入新元素
相关代码:LPUSHX key value

127.0.0.1:6379> lrange name 0 6
Mikky
Coco
Simmons
Tom


127.0.0.1:6379> lpushx name Alice Brown
6

127.0.0.1:6379> lrange name 0 7
Brown
Alice
Mikky
Coco
Simmons
Tom

(5)查看list的值长度
相关代码:LLEN KEY_NAME

127.0.0.1:6379> llen name
6
127.0.0.1:6379> llen number
6

3、举例说明list和hash的应用场景,每个至少一个场景

List的应用场景:论坛评论、秒杀、消息队列

Hash的应用场景:java里结构化的信息存储,例如用户个人信息(姓名、性别、兴趣爱好)用hash结构存储

二、MongoDB作业

1.准备工作

  1. 创建一个数据库 名字grade
MongoDB Enterprise > use grade
switched to db grade
  1. 数据库中创建一个集合名字 class
MongoDB Enterprise > db.createCollection("class");
{ "ok" : 1 }
  1. 集合中插入若干数据 文档格式如下
    {name:‘zhang’,age;10,sex:‘m’,hobby:[‘a’,‘b’,‘c’]}
    hobby: draw sing dance basketball football pingpong computer
    在这里插入图片描述

2. 查找

1、查看班级所有人信息
代码用法:DB.数据库.FIND()

MongoDB Enterprise > db.class.find();
{ "_id" : ObjectId("64a54599a1a01a01ed810f6b"), "name" : "小刚", "age" : 18, "sex" : "m", "hobby" : [ "football", "pingpong", "baskerball", "computer" ] }
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing", "baskerball" ] }
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 19, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64a5461da1a01a01ed810f6e"), "name" : "小政", "age" : 21, "sex" : "m", "hobby" : [ "sing", "basketball", "computer" ] }
{ "_id" : ObjectId("64a5463ea1a01a01ed810f6f"), "name" : "小芳", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing" ] }
{ "_id" : ObjectId("64a5466fa1a01a01ed810f70"), "name" : "小超", "age" : 21, "sex" : "m", "hobby" : [ "pingpong", "sing", "football" ] }
{ "_id" : ObjectId("64a54689a1a01a01ed810f71"), "name" : "小莱", "age" : 22, "sex" : "f", "hobby" : [ "pingpong", "dance", "football" ] }
MongoDB Enterprise > db.class.insert({name:"小猛",age:20,sex:'m',hobby:['basketball','dance','draw']})
WriteResult({ "nInserted" : 1 })

2、查看班级中年龄为18岁的学生信息

MongoDB Enterprise > db.class.find({age:18})
{ "_id" : ObjectId("64a54599a1a01a01ed810f6b"), "name" : "小刚", "age" : 18, "sex" : "m", "hobby" : [ "football", "pingpong", "baskerball", "computer" ] }
{ "_id" : ObjectId("64a54935a1a01a01ed810f78"), "name" : "小白", "age" : 18, "sex" : "f", "hobby" : [ "dance", "sing" ] }
{ "_id" : ObjectId("64a54957a1a01a01ed810f79"), "name" : "小哲", "age" : 18, "sex" : "m", "hobby" : [ "computer", "football", "basketball" ] }

3、查看年龄大于20岁的学生信息

MongoDB Enterprise > db.class.find({age:{$gt:20}})
{ "_id" : ObjectId("64a5461da1a01a01ed810f6e"), "name" : "小政", "age" : 21, "sex" : "m", "hobby" : [ "sing", "basketball", "computer" ] }
{ "_id" : ObjectId("64a5466fa1a01a01ed810f70"), "name" : "小超", "age" : 21, "sex" : "m", "hobby" : [ "pingpong", "sing", "football" ] }
{ "_id" : ObjectId("64a54689a1a01a01ed810f71"), "name" : "小莱", "age" : 22, "sex" : "f", "hobby" : [ "pingpong", "dance", "football" ] }
{ "_id" : ObjectId("64a548e4a1a01a01ed810f73"), "name" : "小刘", "age" : 21, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a5490aa1a01a01ed810f76"), "name" : "小粉", "age" : 23, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }

4、查看年龄在 18-21岁之间的学生信息

MongoDB Enterprise > db.class.find({age:{$gte:18,$lte:21}})
{ "_id" : ObjectId("64a54599a1a01a01ed810f6b"), "name" : "小刚", "age" : 18, "sex" : "m", "hobby" : [ "football", "pingpong", "baskerball", "computer" ] }
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing", "baskerball" ] }
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 19, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64a5461da1a01a01ed810f6e"), "name" : "小政", "age" : 21, "sex" : "m", "hobby" : [ "sing", "basketball", "computer" ] }
{ "_id" : ObjectId("64a5463ea1a01a01ed810f6f"), "name" : "小芳", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing" ] }
{ "_id" : ObjectId("64a5466fa1a01a01ed810f70"), "name" : "小超", "age" : 21, "sex" : "m", "hobby" : [ "pingpong", "sing", "football" ] }
{ "_id" : ObjectId("64a546bba1a01a01ed810f72"), "name" : "小猛", "age" : 20, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548e4a1a01a01ed810f73"), "name" : "小刘", "age" : 21, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548f0a1a01a01ed810f74"), "name" : "小霞", "age" : 20, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a54935a1a01a01ed810f78"), "name" : "小白", "age" : 18, "sex" : "f", "hobby" : [ "dance", "sing" ] }
{ "_id" : ObjectId("64a54957a1a01a01ed810f79"), "name" : "小哲", "age" : 18, "sex" : "m", "hobby" : [ "computer", "football", "basketball" ] }

5、找到年龄为20岁且为男生的学生

MongoDB Enterprise > db.class.find({age:20,sex:'m'})
{ "_id" : ObjectId("64a546bba1a01a01ed810f72"), "name" : "小猛", "age" : 20, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }

6、找到年龄小于20岁或者大于21岁的学生

MongoDB Enterprise > db.class.find({$or:[{age:{$lt:20}},{age:{$gt:21}}]})
{ "_id" : ObjectId("64a54599a1a01a01ed810f6b"), "name" : "小刚", "age" : 18, "sex" : "m", "hobby" : [ "football", "pingpong", "baskerball", "computer" ] }
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 19, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64a54689a1a01a01ed810f71"), "name" : "小莱", "age" : 22, "sex" : "f", "hobby" : [ "pingpong", "dance", "football" ] }
{ "_id" : ObjectId("64a548ffa1a01a01ed810f75"), "name" : "小绿", "age" : 17, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a5490aa1a01a01ed810f76"), "name" : "小粉", "age" : 23, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a54926a1a01a01ed810f77"), "name" : "小青", "age" : 17, "sex" : "m", "hobby" : [ "dance", "draw", "sing" ] }
{ "_id" : ObjectId("64a54935a1a01a01ed810f78"), "name" : "小白", "age" : 18, "sex" : "f", "hobby" : [ "dance", "sing" ] }
{ "_id" : ObjectId("64a54957a1a01a01ed810f79"), "name" : "小哲", "age" : 18, "sex" : "m", "hobby" : [ "computer", "football", "basketball" ] }

7、找到年龄是18岁或者21岁的学生

MongoDB Enterprise > db.class.find({$or:[{age:18},{age:21}]})
{ "_id" : ObjectId("64a54599a1a01a01ed810f6b"), "name" : "小刚", "age" : 18, "sex" : "m", "hobby" : [ "football", "pingpong", "baskerball", "computer" ] }
{ "_id" : ObjectId("64a5461da1a01a01ed810f6e"), "name" : "小政", "age" : 21, "sex" : "m", "hobby" : [ "sing", "basketball", "computer" ] }
{ "_id" : ObjectId("64a5466fa1a01a01ed810f70"), "name" : "小超", "age" : 21, "sex" : "m", "hobby" : [ "pingpong", "sing", "football" ] }
{ "_id" : ObjectId("64a548e4a1a01a01ed810f73"), "name" : "小刘", "age" : 21, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a54935a1a01a01ed810f78"), "name" : "小白", "age" : 18, "sex" : "f", "hobby" : [ "dance", "sing" ] }
{ "_id" : ObjectId("64a54957a1a01a01ed810f79"), "name" : "小哲", "age" : 18, "sex" : "m", "hobby" : [ "computer", "football", "basketball" ] }

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

MongoDB Enterprise > db.class.find({$where:"this.hobby.length == 2"})
{ "_id" : ObjectId("64a5463ea1a01a01ed810f6f"), "name" : "小芳", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing" ] }
{ "_id" : ObjectId("64a54935a1a01a01ed810f78"), "name" : "小白", "age" : 18, "sex" : "f", "hobby" : [ "dance", "sing" ] }

9、找到兴趣爱好有draw的学生

MongoDB Enterprise > db.class.find({hobby:'draw'})
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing", "baskerball" ] }
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 19, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64a5463ea1a01a01ed810f6f"), "name" : "小芳", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing" ] }
{ "_id" : ObjectId("64a546bba1a01a01ed810f72"), "name" : "小猛", "age" : 20, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548e4a1a01a01ed810f73"), "name" : "小刘", "age" : 21, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548f0a1a01a01ed810f74"), "name" : "小霞", "age" : 20, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548ffa1a01a01ed810f75"), "name" : "小绿", "age" : 17, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a5490aa1a01a01ed810f76"), "name" : "小粉", "age" : 23, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a54926a1a01a01ed810f77"), "name" : "小青", "age" : 17, "sex" : "m", "hobby" : [ "dance", "draw", "sing" ] }

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

MongoDB Enterprise > db.class.find({hobby:{$all:['dance','draw']}})
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 19, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64a546bba1a01a01ed810f72"), "name" : "小猛", "age" : 20, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548e4a1a01a01ed810f73"), "name" : "小刘", "age" : 21, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548f0a1a01a01ed810f74"), "name" : "小霞", "age" : 20, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548ffa1a01a01ed810f75"), "name" : "小绿", "age" : 17, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a5490aa1a01a01ed810f76"), "name" : "小粉", "age" : 23, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a54926a1a01a01ed810f77"), "name" : "小青", "age" : 17, "sex" : "m", "hobby" : [ "dance", "draw", "sing" ] }

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

MongoDB Enterprise > db.class.find({$where:"this.hobby.length == 3"})
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing", "baskerball" ] }
{ "_id" : ObjectId("64a5461da1a01a01ed810f6e"), "name" : "小政", "age" : 21, "sex" : "m", "hobby" : [ "sing", "basketball", "computer" ] }
{ "_id" : ObjectId("64a5466fa1a01a01ed810f70"), "name" : "小超", "age" : 21, "sex" : "m", "hobby" : [ "pingpong", "sing", "football" ] }
{ "_id" : ObjectId("64a54689a1a01a01ed810f71"), "name" : "小莱", "age" : 22, "sex" : "f", "hobby" : [ "pingpong", "dance", "football" ] }
{ "_id" : ObjectId("64a546bba1a01a01ed810f72"), "name" : "小猛", "age" : 20, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548e4a1a01a01ed810f73"), "name" : "小刘", "age" : 21, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548f0a1a01a01ed810f74"), "name" : "小霞", "age" : 20, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a548ffa1a01a01ed810f75"), "name" : "小绿", "age" : 17, "sex" : "m", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a5490aa1a01a01ed810f76"), "name" : "小粉", "age" : 23, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "_id" : ObjectId("64a54926a1a01a01ed810f77"), "name" : "小青", "age" : 17, "sex" : "m", "hobby" : [ "dance", "draw", "sing" ] }
{ "_id" : ObjectId("64a54957a1a01a01ed810f79"), "name" : "小哲", "age" : 18, "sex" : "m", "hobby" : [ "computer", "football", "basketball" ] }

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

MongoDB Enterprise > db.class.find({}).sort({age:-1}).skip(1).limit(1)
{ "_id" : ObjectId("64a54689a1a01a01ed810f71"), "name" : "小莱", "age" : 22, "sex" : "f", "hobby" : [ "pingpong", "dance", "football" ] }

13、查看学生的兴趣范围

MongoDB Enterprise > db.class.distinct('hobby')
[
	"baskerball",
	"computer",
	"football",
	"pingpong",
	"draw",
	"sing",
	"dance",
	"basketball"
]

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

MongoDB Enterprise > db.class.find({},{_id:0}).sort({age:-1}).limit(3)
{ "name" : "小粉", "age" : 23, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ] }
{ "name" : "小莱", "age" : 22, "sex" : "f", "hobby" : [ "pingpong", "dance", "football" ] }
{ "name" : "小政", "age" : 21, "sex" : "m", "hobby" : [ "sing", "basketball", "computer" ] }

15、删除所有 年级大于20或者小于18岁的学生

MongoDB Enterprise > db.class.deleteMany({$or:[{age:{$gt:20}},{age:{$lt:5}}]})
{ "acknowledged" : true, "deletedCount" : 5 }

增加、更新、删除、统计

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

MongoDB Enterprise > db.class.update({'name':'小红'},{$set:{'age':8,'hobby':['dance','draw']}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.class.find({'name':'小红'})
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 8, "sex" : "f", "hobby" : [ "dance", "draw" ] }

2、追加小白兴趣爱好 唱歌

MongoDB Enterprise > db.class.update({'name':'小白'},{$push:{'hobby':'sing'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

3、小哲兴趣爱好增加 吹牛 打篮球

MongoDB Enterprise > db.class.update({'name':'小哲'},{$push:{'hobby':{$each:['basketball','吹牛']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

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

MongoDB Enterprise > db.class.update({'name':'小珍'},{$addToSet:{hobby:{$each:['running','sing']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

5、该班所有同学年龄加1

MongoDB Enterprise > db.class.update({},{$inc:{age:1}},false,true)
WriteResult({ "nMatched" : 10, "nUpserted" : 0, "nModified" : 10 })

6、删除小白的sex属性

MongoDB Enterprise > db.class.update({'name':'小白'},{$unset:{sex:0}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

7、删除小哲兴趣中的第一项

MongoDB Enterprise > db.class.update({'name':'小哲'},{$pop:{hobby:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

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

MongoDB Enterprise > db.class.update({'name':'小红'},{$pull:{hobby:'draw'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

MongoDB Enterprise > db.class.find({'name':'小红'})
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "dance" ] }

3、增加分数域 score:{‘chinese’:88,‘english’:78,‘math’:98}

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

MongoDB Enterprise > db.class.aggregate([ { $group: { _id: "$sex", count: { $sum: 1 } } }])
{ "_id" : null, "count" : 1 }
{ "_id" : "f", "count" : 4 }
{ "_id" : "m", "count" : 5 }

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

MongoDB Enterprise > db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])

3、统计每名男生的语文成绩

MongoDB Enterprise > db.class.aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])
{ "name" : "小刚", "score" : { "chinese" : 80 } }
{ "name" : "小猛", "score" : { "chinese" : 89 } }
{ "name" : "小绿", "score" : { "chinese" : 78 } }
{ "name" : "小青", "score" : { "chinese" : 75 } }
{ "name" : "小哲", "score" : { "Chinese" : 80 } }

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

MongoDB Enterprise > db.class.aggregate([{$match:{sex:'f'}},{$sort:{'score.english':-1}}])

{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "dance" ], "score" : { "chinese" : 95, "english" : 90, "math" : 85 } }
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer", "running" ], "score" : { "chinese" : 93, "english" : 88, "math" : 94 } }
{ "_id" : ObjectId("64a548f0a1a01a01ed810f74"), "name" : "小霞", "age" : 21, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ], "score" : { "chinese" : 92, "english" : 85, "math" : 76 } }
{ "_id" : ObjectId("64a5463ea1a01a01ed810f6f"), "name" : "小芳", "age" : 21, "sex" : "f", "hobby" : [ "draw", "sing" ], "score" : { "chinese" : 86, "english" : 80, "math" : 90 } }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/766340.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前端开发报表工具所必须的三大能力

摘要:本文由葡萄城技术团队于CSDN原创并首发。转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具、解决方案和服务,赋能开发者。 数据分析一直以来都是业务决策中非常重要的一环,在数字化时代尤其如此。然…

轻松实现文件夹批量重命名!教你如何使用顺序编号批量改名文件夹!

我们需要对大量文件夹进行重命名时,手动一个个改名无疑是一项耗时且繁琐的任务。不过,别担心!我们将向您介绍一种简单而高效的方法来实现文件夹的批量重命名:使用顺序编号。无论是对照片文件夹、音乐文件夹、还是其他文件夹进行改…

cuda 安装教程(win10)

cuda:compute unitifed device architecture-统一计算设备架构。 NVIDIA cuDNN is a GPU-accelerated library of primitives for deep neural networks. 目录 cuda完整安装: 检查显卡安装版本 下载版本 cuda安装步骤 cudnn下载安装(需要登陆账号&…

(2023.07.05-2023.07.15)论文阅读简单记录和汇总

(2023.07.05-2023.07.15)论文阅读简单记录和汇总 2023/07/05:端午回家还没玩几天就被老板召唤回学校了,采购的事情真是太麻烦了,一堆的差错。昨天跟师弟把他的第一篇论文投出去了,祝好运! 2023/07/10:可惜…

基于Java+SpringBoot+vue前后端分离社区团购系统设计实现

博主介绍:✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 🍅文末获取源码联系🍅 👇🏻 精彩专…

【Ceph的部署】

目录 一、基于 ceph-deploy 部署 Ceph 集群1、Ceph 生产环境推荐:2、Ceph 环境规划3、环境准备1、关闭 selinux 与防火墙2、根据规划设置主机名3、配置 hosts 解析4、安装常用软件和依赖包5、在 admin 管理节点配置 ssh 免密登录所有节点6、为每一个服务器配置时间同…

【129. 求根节点到叶节点数字之和

129. 求根节点到叶节点数字之和 原题链接:完成情况:解题思路:参考代码: 原题链接: 129. 求根节点到叶节点数字之和 https://leetcode.cn/problems/sum-root-to-leaf-numbers/ 完成情况: 解题思路&#x…

vue2项目使用?.语法报错如何解决?(@babel/plugin-proposal-optional-chaining)

文章目录 一、问题原因二、下载并配置插件第一步第二步第三步 一、问题原因 因为有些浏览器版本不兼容?.语法,可以使用$$来代替(如下图所示),但是为了团队协作避免麻烦使用?.带来的问题,可以使用这个(babel/plugin-p…

科技资讯|Apple Vision Pro新专利,关于相对惯性测量系统的校正

美国专利商标局正式授予苹果一项 Apple Vision Pro 相关专利,该专利涵盖了具有视觉校正功能的相对惯性测量系统。这样的系统用于弥补头显内的眼前庭不匹配,当 VR 头显中发生的事情与现实世界环境中发生的运动不匹配时,可能会导致恶心。 苹果…

web(三)前端

1.选择器: 1.伪类选择器: 伪选择器支持按照未被包含在文档树中的状态信息来选择元素超链接元素的四种伪类:1.link:表示没有访问过的的链接2.visited:表示访问过的链接3.hover:将鼠标移动至超链接上时超链接的状态4.active:鼠标点击超链接状态…

IDR: Self-Supervised Image Denoising via Iterative Data Refinement

文章目录 IDR: Self-Supervised Image Denoising via Iterative Data Refinement1. noisy-clean pair 比较难获取2. noiser-noisy pair 比较容易获取,但是训练效果呢?2.1 noiser-noisy 训练的模型,能够对 noisy 图像一定程度的降噪2.2 noiser…

基于Nginx深入浅出亿级流量架构设计(持续更新2023.7.18)

基于Nginx深入浅出亿级流量架构设计 环境准备/安装部署Nginx四个发行版本简单介绍Nginx的安装 Nginx的目录结构与基本运行原理及其最小配置解析Nginx虚拟主机与域名配置ServerName匹配规则反向代理在系统结构中的应用场景Nginx的反向代理配置 环境准备/安装部署 虚拟机使用VMw…

STM32F4_FLASH模拟EEPROM

目录 前言 1. 内部FLASH简介 2. 内部FLASH写入过程 3. 内部FLASH库函数 4. FLASH的读写保护及解除 5. FLASH相关寄存器 6. 实验程序 6.1 main.c 6.2 STMFlash.c 6.3 STMFlash.h 前言 STM32F4本身并没有自带EEPROM,但是STM32F4具有IAP功能,也就…

初阶数据结构——排序

目录 排序的概念常见排序算法插入排序希尔排序选择排序堆排序冒泡排序快速排序hoare挖坑法前后指针法快排的时间复杂度三路划分三数取中和随机数选中快排的非递归版本快速排序的总结 归并排序归并的递归版本归并的非递归版本 内排序和外排序非比较排序稳定性排序算法复杂度和稳…

测试基础 Android 应用测试总结

目录 启动: 功能介绍,引导图,流量提示等: 权限: 文件错误 屏幕旋转: 流量: 缓存(/sdcard/data/com.your.package/cache/): 正常中断: 异…

Android 自定义CheckBox样式,设置切换背景图,类似于RadioButton

文章目录 概要自定义CheckBok资源文件如下使用方法实现效果 概要 目前要实现类似于Radiobutton选择按钮,如果只有一个RadioButton,就不能和radio Group连用,导致选择没办法取消,如果要实现只能代码中进行操作,过于繁琐…

项目管理软件选择指南:最佳实践与避坑指南

当今企业中,协作工具是必不可少的,每个企业都会寻找最适合自己的协作工具来提高工作效率。在这些协作工具中,Zoho Projects项目协作工具是最常用的一种,因为它能够为团队提供一个集任务、项目、文档、IM、目标、日历、甘特图、工时…

MIT 6.829 -- L2 The Internetworking Problem

MIT 6.829 -- L2 The Internetworking Problem 前言The Internetworking Problem: Many Different NetworksGateWays互联网设计原则通用性原则健壮性原则互联网缺点互联网协议标准流程 最早的TCP/IP今天的TCP/IP: IPv4地址分片和重组Time-to-live(TTL)Ty…

2023 7.17~7.23 周报 (最近读的论文方法论分析)

0 上周回顾 上周完成了RTM的研究学习, 完成了进一步阅读论文所需的知识储备. 同时从代码层面深度解析了正演和RTM存在的关系, 发掘了很多富有参考意义的信息. 1 本周计划 深度剖析论文《Deep-Learning Full-Waveform Inversion Using Seismic Migration Images》的方法体系,…

计算机网络——VLan介绍

学习视频: 网工必会,十分钟搞明白,最常用的VLAN技术_哔哩哔哩_bilibili 技术总结:VLAN,网络中最常用的技术,没有之一_哔哩哔哩_bilibili 全国也没几个比我讲得好的:VLAN虚拟局域网 本来补充了…