欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python爬虫25-MongoDB练习题

程序员文章站 2022-05-01 19:53:54
MongoDB练习题测试数据1、查询年龄大于25小于27的name,age2、查询出不是美国的name3、查询国籍是中国或者美国的学生信息4、查询语文成绩大于85或者英语成绩大于90的学生信息5、查询出名字中存在"li"的学生信息6、查询喜欢看MONGODB和PHP的学生7、查询第二本书是JAVA的学生信息8、查询喜欢的书数量是4本的学生9、查询出persons中一共有多少国家分别是什么测试数据var persons = [{name:"jim",age:25,email:"75431457...

测试数据

var persons = [{
	name:"jim",
	age:25,
	email:"75431457@qq.com",
	c:89,m:96,e:87,
	country:"USA",
	books:["JS","C++","EXTJS","MONGODB"]
},
{
	name:"tom",
	age:25,
	email:"214557457@qq.com",
	c:75,m:66,e:97,
	country:"USA",
	books:["PHP","JAVA","EXTJS","C++"]
},
{
	name:"lili",
	age:26,
	email:"344521457@qq.com",
	c:75,m:63,e:97,
	country:"USA",
	books:["JS","JAVA","C#","MONGODB"]
},
{
	name:"zhangsan",
	age:27,
	email:"2145567457@qq.com",
	c:89,m:86,e:67,
	country:"China",
	books:["JS","JAVA","EXTJS","MONGODB"]
},
{
	name:"lisi",
	age:26,
	email:"274521457@qq.com",
	c:53,m:96,e:83,
	country:"China",
	books:["JS","C#","PHP","MONGODB"]
},
{
	name:"wangwu",
	age:27,
	email:"65621457@qq.com",
	c:45,m:65,e:99,
	country:"China",
	books:["JS","JAVA","C++","MONGODB"]
},
{
	name:"zhaoliu",
	age:27,
	email:"214521457@qq.com",
	c:99,m:96,e:97,
	country:"China",
	books:["JS","JAVA","EXTJS","PHP"]
},
{
	name:"piaoyingjun",
	age:26,
	email:"piaoyingjun@uspcat.com",
	c:39,m:54,e:53,
	country:"Korea",
	books:["JS","C#","EXTJS","MONGODB"]
},
{
	name:"lizhenxian",
	age:27,
	email:"lizhenxian@uspcat.com",
	c:35,m:56,e:47,
	country:"Korea",
	books:["JS","JAVA","EXTJS","MONGODB"]
},
{
	name:"lixiaoli",
	age:21,
	email:"lixiaoli@uspcat.com",
	c:36,m:86,e:32,
	country:"Korea",
	books:["JS","JAVA","PHP","MONGODB"]
},
{
	name:"zhangsuying",
	age:22,
	email:"zhangsuying@uspcat.com",
	c:45,m:63,e:77,
	country:"Korea",
	books:["JS","JAVA","C#","MONGODB"]
}]

for(var i = 0;i<persons.length;i++){
	db.persons.insert(persons[i])
}

var persons = db.persons.find({name:"jim"})
while(persons.hasNext()){
	obj = persons.next();
        print(obj.books.length)
}

1、查询年龄大于25小于27的name,age

db.persons.find({age:{$gt:25,$lt:27}},{name:1,age:1,_id:0})

Python爬虫25-MongoDB练习题

2、查询出不是美国的name

db.persons.find({country:{$ne:'USA'}},{name:1,country:1,_id:0})

Python爬虫25-MongoDB练习题

3、查询国籍是中国或者美国的学生信息

db.persons.find({$or:[{country:'China'},{country:'USA'}]},{name:1,country:1,_id:0})

Python爬虫25-MongoDB练习题

4、查询语文成绩大于85或者英语成绩大于90的学生信息

c:语文成绩
e:英语成绩

db.persons.find({$or:[{c:{$gt:85}},{e:{$gt:90}}]},{c:1,e:1,name:1,_id:0})

Python爬虫25-MongoDB练习题

5、查询出名字中存在"li"的学生信息

第一种

db.persons.find({name:/li/},{name:1,_id:0})

Python爬虫25-MongoDB练习题
第二种

db.persons.find({name:/l.*?i/},{name:1,_id:0})

Python爬虫25-MongoDB练习题

6、查询喜欢看MONGODB和PHP的学生

$all:所有包含的

 db.persons.find({books:{$all:['MONGODB','PHP']}},{name:1,books:1,_id:0})

Python爬虫25-MongoDB练习题

7、查询第二本书是JAVA的学生信息

利用索引下标查询
第二本:‘books.1’

db.persons.find({'books.1':'JAVA'},{name:1,books:1,_id:0})

Python爬虫25-MongoDB练习题

8、查询喜欢的书数量是4本的学生

$size:数量

db.persons.find({books:{$size:4}},{name:1,books:1,_id:0})

Python爬虫25-MongoDB练习题

9、查询出persons中一共有多少国家分别是什么

distinct:返回唯一不同的值,去重

db.persons.distinct('country')

Python爬虫25-MongoDB练习题

本文地址:https://blog.csdn.net/weixin_47133012/article/details/108723595

相关标签: python mongodb