mongoDB练习强化
作者:互联网
关于使用MongoDB的强化练习
练习示例如下:
mongo
use test
首先准备一个集合的数据。
persons = [{
name:"jim",
age:25,
email:"7543xx57@qq.com",
c:89,m:96,e:87,
country:"USA",
books:["JS","C++","EXTJS","MONGODB"]
},
{
name:"tom",
age:25,
email:"2145xx457@qq.com",
c:75,m:66,e:97,
country:"USA",
books:["PHP","JAVA","EXTJS","C++"]
},
{
name:"lili",
age:26,
email:"344xxx457@qq.com",
c:75,m:63,e:97,
country:"USA",
books:["JS","JAVA","C#","MONGODB"]
},
{
name:"zhangsan",
age:27,
email:"2xxx567457@qq.com",
c:89,m:86,e:67,
country:"China",
books:["JS","JAVA","EXTJS","MONGODB"]
},
{
name:"lisi",
age:26,
email:"27xx21457@qq.com",
c:53,m:96,e:83,
country:"China",
books:["JS","C#","PHP","MONGODB"]
},
{
name:"wangwu",
age:27,
email:"65xxx1457@qq.com",
c:45,m:65,e:99,
country:"China",
books:["JS","JAVA","C++","MONGODB"]
},
{
name:"zhaoliu",
age:27,
email:"21xxx1457@qq.com",
c:99,m:96,e:97,
country:"China",
books:["JS","JAVA","EXTJS","PHP"]
},
{
name:"piaoyingjun",
age:26,
email:"piaoyingjunx@uspcat.com",
c:39,m:54,e:53,
country:"Korea",
books:["JS","C#","EXTJS","MONGODB"]
},
{
name:"lizhenxian",
age:27,
email:"lizhenxianx@uspcat.com",
c:35,m:56,e:47,
country:"Korea",
books:["JS","JAVA","EXTJS","MONGODB"]
},
{
name:"lixiaoli",
age:21,
email:"lixiaolixx@uspcat.com",
c:36,m:86,e:32,
country:"Korea",
books:["JS","JAVA","PHP","MONGODB"]
},
{
name:"zhangsuying",
age:22,
email:"zhangsuyingxx@uspcat.com",
c:45,m:63,e:77,
country:"Korea",
books:["JS","JAVA","C#","MONGODB"]
}]
将数据插入集合persons中
db.persons.insert(persons)
db.persons.find()
一、查询年龄大于25小于27的name,age
db.persons.find({age:{$gt:25,$lt:27}},{name:1,age:1,_id:0})
二、查询出country不是美国的name和country数据
①
db.persons.find({country:{$ne:"USA"}},{name:1,country:1,_id:0})
②
db.persons.find({country:{$nin:["USA"]}},{name:1,country:1,_id:0})
三、查询country是China或者USA的 name、age、country数据信息
①
db.persons.find({$or:[{country:{$eq:"USA"}},{country:{$eq:"China"}}]},{name:1,age:1,country:1,_id:0})
②
db.persons.find({country:{$in:["China","USA"]}},{name:1,age:1,country:1,_id:0})
四、查询语文(字段c)成绩大于85或者英语(字段e)成绩大于90的name、age、country、c、e数据信息
db.persons.find({$or:[{c:{$gt:85}},{e:{$gt:90}}]},{name:1,age:1,country:1,c:1,e:1,_id:0})
五、查询出名字中存在"li"的学生信息
这里使用了正则表达式
db.persons.find({name:/li/},{name:1,_id:0})
其中,两个斜杠/ 之间写的是正则表达式
拓展
- 不区分大小写 :
在斜杠后加一个小写字母i
db.persons.find({name:/li/i},{name:1,_id:0})
- 以某字符开头
name以字符p开头为例
db.persons.find({name:/^p/},{name:1,_id:0})
- 以某字符结尾
name以字符u结尾
db.persons.find({name:/u$/},{name:1,_id:0})
六、查询喜欢看MONGODB和PHP的学生
- 查找值为列表的字段,其列表中的元素
db.persons.find({books:{$all:["MONGODB","PHP"]}},{name:1,books:1,_id:0})
七、查询第二本书是JAVA的学生信息
即查询字段books的列表中第二个元素为JAVA的人
db.persons.find({"books.1":"JAVA"},{name:1,books:1,_id:0})
"books.1"表示books的第二个元素,索引从0开始计数。
- 拓展
向books列表中添加一个元素
向name为jim的数据的books的列表中添加一个元素"HADOOP"。
db.persons.update({name:"jim"},{$push:{books:"HADOOP"}})
向name为jim的数据的books的列表中添加多个元素"SPARK",“HBASE”。
db.persons.update({name:"jim"},{$push:{books:{$each:["SPARK","HBASE"]}}})
八、查询喜欢的书数量是4本的学生
db.persons.find({books:{$size: 4}},{name:1,books:1,_id:0})
九、查询出persons中的国家分别是什么
db.persons.distinct("country")
如果想查看长度:
db.persons.distinct("country").length
标签:name,mongoDB,age,练习,db,persons,books,country,强化 来源: https://blog.csdn.net/weixin_48964486/article/details/123615943