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


文章目录

  • 前言
  • 一、在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
0127.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
Tom127.0.0.1:6379> lpushx name Alice Brown
6127.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.mzph.cn/news/2795.shtml

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

相关文章

echarts图例对齐

富文本不生效,是没有设置lineHeight

企业内部FAQ系统的搭建重要性是什么?

企业内部FAQ系统(Frequently Asked Questions,常见问题解答系统)的搭建对于企业来说具有重要的意义。它可以帮助企业有效地管理和解决员工和客户的常见问题,提高工作效率和服务质量。 企业内部FAQ系统搭建的重要性: …

Python批量实现Word、EXCLE、PPT转PDF文件

一、绪论背景 在日常办公和文档处理中,有时我们需要将多个Word文档、Excel表格或PPT演示文稿转换为PDF文件。将文档转换为PDF格式的好处是它可以保留文档的布局和格式,并且可以在不同平台上进行方便的查看和共享。 本篇博文将介绍如何使用Python编程语言…

lua脚本语言学习笔记

Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。 因为我们使用redis的时候一般要写lua脚本,这篇文章就介绍一下lua脚本语言的基础用…

旅行社优惠卡app软件开发

旅游行业的不断发展,越来越多的旅行社开始推出各种优惠卡来吸引游客。而随着智能手机的普及,开发一款旅行社优惠卡APP软件成为了一种必然的趋势。 该软件的主要功能是提供旅行社的各种优惠卡信息,包括优惠卡的种类、价格、使用范围、有效…

Pytorch如何打印与Keras的model.summary()类似的输出

1 Keras的model.summary() 2 Pytorch实现 2.1 安装torchsummary包 pip install torchsummary2.2 代码 import torch import torch.nn as nn import torch.nn.functional as F from torchsummary import summaryclass Net(nn.Module):def __init__(self):super(Net, self).__…

【Spring Boot学习一】创建项目 Spring Boot的配置文件

目录 一、安装插件 二、创建Spring Boot项目 1、创建项目 1.1 使用IDEA创建 1.2 网页版本创建 2、项目目录介绍与运行 三、Sping Boot的配置文件(重点) 🌷1、.properties配置文件 (1)基础语法:Key …

我在VScode学Java类与对象(Java显式参数和隐式参数、静态方法+main方法、Java访问修饰符、static关键字、Java的包、对象数组)第三辑

我的个人博客主页:如果’真能转义1️⃣说1️⃣的博客主页 关于Java基本语法学习---->可以参考我的这篇博客:《我在VScode学Java》 续《我在VScode学Java(Java的类与对象)》 方法会操作对象并访问他们的实例字段。 伍._. 显式参…

elementUI el-radio 无法点击的问题

<el-form-item label"B端客户类型" prop"user_type"><template slot"label"><span>B端客户类型</span><el-tooltip effect"dark" placement"top" content"B端大客户账期有效,只有设置该类型…

javascript实现久久乘法口诀表、document、write、console、log

文章目录 正序乘法口诀表倒序乘法口诀表logconsoledocumentwrite 正序乘法口诀表 function multiplicationTable() {for (let i 1; i < 9; i) {let val ;for (let j 1; j < i; j) {document.write(j * i (i * j) &nbsp );val ${j}*${i}${i * j} ;}consol…

Vue第三篇:最简单的vue购物车示例

本文参考&#xff1a;Vue Cli&#xff08;脚手架&#xff09;实现购物车小案例 - - php中文网博客 效果图&#xff1a; 编写流程&#xff1a; 1、首先通过vue/cli创建工程 vue create totalprice 2、改写App.vue代码如下&#xff1a; <template><div><div v…

深入浅出对话系统——基于预训练语言模型的对话管理

引言 主要讲解三篇论文&#xff0c;主要思想是把自然语言理解、对话管理和自然语言生成三部分整合到一起。 先导知识 数据集 CamRest676MultiWOZ 都是用的自回归语言模型 causalGPT-2、Transformer Decoder 一个概念&#xff1a;delexicalization 通过相应的占位符替换…

基于IMX6ULL的AP3216C的QT动态数据曲线图显示

前言&#xff1a;本文为手把手教学 LinuxQT 的典型基础项目 AP3216C 的数据折线图显示&#xff0c;项目使用正点原子的 IMX6ULL 阿尔法( Cortex-A7 系列)开发板。项目需要实现 AP3216C 在 Linux 系统下的驱动&#xff0c;使用 QT 设计 AP3216C 的数据显示页面作为项目的应用层。…

websocket发布到iis之后无法创建连接访问

在做websocket测试时在本地的服务可以正常访问&#xff0c;在调试成功之后发布到了iis中&#xff0c;发现无法连接&#xff0c;这时可能有以下几个原因。 WebSocket无法连接到IIS&#xff1a; IIS版本不支持WebSocket&#xff1a;IIS 8.0及更高版本才支持WebSocket&#xff0…

从小白到大神之路之学习运维第61天--------Ansible自动化运维工具(playbook配置文件深入了解)

第三阶段基础 时 间&#xff1a;2023年7月14日 参加人&#xff1a;全班人员 内 容&#xff1a; playbook配置文件 目录 playbook配置文件 一、playbook配置文件概念 修改hosts文件 建立playbook配置文件 yml脚本写法注释&#xff1a; 二、Playbook的核心元素 三、…

如何破解中小企业数字化转型难点?建议来了!

打开任何一个搜索引擎&#xff0c;只要输入“中小企业数字化转型”&#xff0c;关于痛点、难处的文章就会铺面而来&#xff0c;难在哪里&#xff0c;其实很好解答&#xff0c;关键在于&#xff0c;如何解决这一个个难处。 PS&#xff1a;给大家整理了一份完整版的《中小企业如…

汽车供应链专场对接会 | 8月25日大会同期活动

爱普搜汽车供应链对接会&#xff0c;是根据采购商的项目需求&#xff0c;有针对性地组织全国各地采购商与供应商&#xff0c;进行面对面交流与沟通&#xff0c;促成实质性交易。参会群体为汽车行业制造型企业、主机厂、Tier1/2。 供应商在参加对接会前已做足功课&#xff0c;现…

C语言-ubuntu下的命令

目录 linux命令 【1】打开关闭终端 【2】终端 【3】ls命令 【4】cd 切换路径 【5】新建 【6】删除 【7】复制 【8】移动 【9】常用快捷键 【10】vi编辑器 【11】简单编程步骤 任务&#xff1a; linux命令 【1】打开关闭终端 打开终端&#xff1a; 1. 直接点击 …

File格式转换MultipartFile格式的例子

首先&#xff1a;需要先引入依赖包 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.9</version> </dependency> 1.Multipartfile转File类型 //创建一…

一零六四、世界杯数据可视化分析(阿里云天池赛)

目录 赛制官方链接 活动背景 活动时间&#xff1a;即日起-12月31日17点 数据说明 世界杯成绩信息表&#xff1a;WorldCupsSummary 世界杯比赛比分汇总表&#xff1a;WorldCupMatches.csv 世界杯球员信息表&#xff1a;WorldCupPlayers.csv 代码实现 赛制官方链接 世界杯…