MongoDB Shell基本操作(四) - 高级查询

新建students集合并插入样本文档:

db.students.insert({Name:"S1",Age:25,Gender:"M",Class:"C1",Score:95})
db.students.insert({Name:"S2",Age:18,Gender:"M",Class:"C1",Score:85})
db.students.insert({Name:"S3",Age:18,Gender:"F",Class:"C1",Score:85})
db.students.insert({Name:"S4",Age:18,Gender:"F",Class:"C1",Score:75})
db.students.insert({Name:"S5",Age:18,Gender:"F",Class:"C2",Score:75})
db.students.insert({Name:"S6",Age:21,Gender:"M",Class:"C2",Score:100})
db.students.insert({Name:"S7",Age:21,Gender:"M",Class:"C2",Score:100})
db.students.insert({Name:"S8",Age:25,Gender:"F",Class:"C2",Score:100})
db.students.insert({Name:"S9",Age:25,Gender:"F",Class:"C2",Score:90})
db.students.insert({Name:"S10",Age:28,Gender:"F",Class:"C3",Score:90})

1、使用条件操作符

1.1 $lt和$lte

Age小于25岁的学生:

> db.students.find({"Age":{"$lt":25}})
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c2"), "Name" : "S2", "Age" : 18, "Gender" : "M", "Class" : "C1", "Score" : 85 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c3"), "Name" : "S3", "Age" : 18, "Gender" : "F", "Class" : "C1", "Score" : 85 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c4"), "Name" : "S4", "Age" : 18, "Gender" : "F", "Class" : "C1", "Score" : 75 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c5"), "Name" : "S5", "Age" : 18, "Gender" : "F", "Class" : "C2", "Score" : 75 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c6"), "Name" : "S6", "Age" : 21, "Gender" : "M", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c7"), "Name" : "S7", "Age" : 21, "Gender" : "M", "Class" : "C2", "Score" : 100 }

Age小于等于25岁的学生:

> db.students.find({"Age":{"$lte":25}})
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c1"), "Name" : "S1", "Age" : 25, "Gender" : "M", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c2"), "Name" : "S2", "Age" : 18, "Gender" : "M", "Class" : "C1", "Score" : 85 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c3"), "Name" : "S3", "Age" : 18, "Gender" : "F", "Class" : "C1", "Score" : 85 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c4"), "Name" : "S4", "Age" : 18, "Gender" : "F", "Class" : "C1", "Score" : 75 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c5"), "Name" : "S5", "Age" : 18, "Gender" : "F", "Class" : "C2", "Score" : 75 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c6"), "Name" : "S6", "Age" : 21, "Gender" : "M", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c7"), "Name" : "S7", "Age" : 21, "Gender" : "M", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c8"), "Name" : "S8", "Age" : 25, "Gender" : "F", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c9"), "Name" : "S9", "Age" : 25, "Gender" : "F", "Class" : "C2", "Score" : 90 }

1.2 $gt和$gte

Age大于25岁的学生:

> db.students.find({"Age":{"$gt":25}})
{ "_id" : ObjectId("5afc02ba8dda484c0dacf0ca"), "Name" : "S10", "Age" : 28, "Gender" : "F", "Class" : "C3", "Score" : 90 }

Age大于等于25岁的学生:

> db.students.find({"Age":{"$gte":25}})
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c1"), "Name" : "S1", "Age" : 25, "Gender" : "M", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c8"), "Name" : "S8", "Age" : 25, "Gender" : "F", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c9"), "Name" : "S9", "Age" : 25, "Gender" : "F", "Class" : "C2", "Score" : 90 }
{ "_id" : ObjectId("5afc02ba8dda484c0dacf0ca"), "Name" : "S10", "Age" : 28, "Gender" : "F", "Class" : "C3", "Score" : 90 }

1.3 $in和$nin

C1和C2班所有的学生:

> db.students.find({"Class":{"$in":["C1","C2"]}})
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c1"), "Name" : "S1", "Age" : 25, "Gender" : "M", "Class" : "C1", "Score" : 95 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c2"), "Name" : "S2", "Age" : 18, "Gender" : "M", "Class" : "C1", "Score" : 85 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c3"), "Name" : "S3", "Age" : 18, "Gender" : "F", "Class" : "C1", "Score" : 85 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c4"), "Name" : "S4", "Age" : 18, "Gender" : "F", "Class" : "C1", "Score" : 75 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c5"), "Name" : "S5", "Age" : 18, "Gender" : "F", "Class" : "C2", "Score" : 75 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c6"), "Name" : "S6", "Age" : 21, "Gender" : "M", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c7"), "Name" : "S7", "Age" : 21, "Gender" : "M", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c8"), "Name" : "S8", "Age" : 25, "Gender" : "F", "Class" : "C2", "Score" : 100 }
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c9"), "Name" : "S9", "Age" : 25, "Gender" : "F", "Class" : "C2", "Score" : 90 }

不属于C1和C2班所有的学生:

扫描二维码关注公众号,回复: 3278813 查看本文章
> db.students.find({"Class":{"$nin":["C1","C2"]}})
{ "_id" : ObjectId("5afc02ba8dda484c0dacf0ca"), "Name" : "S10", "Age" : 28, "Gender" : "F", "Class" : "C3", "Score" : 90 }

1.4 综合

性别是“M”或者属于“C1”或“C2”班并且年龄大于或等于25岁的所有学生:

> db.students.find({$or:[{"Gender":"M","Class":{"$in":["C1","C2"]}}],"Age":{"$gte":25}})
{ "_id" : ObjectId("5afc02b98dda484c0dacf0c1"), "Name" : "S1", "Age" : 25, "Gender" : "M", "Class" : "C1", "Score" : 95 }

2、正则表达式

添加样本文档:

db.students.insert({Name:"Student1",Age:30,Gender:"M",Class:"Biology",Score:90})
db.students.insert({Name:"Student2",Age:30,Gender:"M",Class:"Chemistry",Score:90})
db.students.insert({Name:"Test1",Age:30,Gender:"M",Class:"Chemistry",Score:90})
db.students.insert({Name:"Test2",Age:30,Gender:"M",Class:"Chemistry",Score:90})
db.students.insert({Name:"Test3",Age:30,Gender:"M",Class:"Chemistry",Score:90})

查询姓名以“St”或“Te”开头,并且其班级以“Che”开头的所有学生:

> db.students.find({"Name":/(St|Te)*/i, "Class":/(Che)/i})
{ "_id" : ObjectId("5afc076d8dda484c0dacf0cc"), "Name" : "Student2", "Age" : 30, "Gender" : "M", "Class" : "Chemistry", "Score" : 90 }
{ "_id" : ObjectId("5afc076d8dda484c0dacf0cd"), "Name" : "Test1", "Age" : 30, "Gender" : "M", "Class" : "Chemistry", "Score" : 90 }
{ "_id" : ObjectId("5afc076d8dda484c0dacf0ce"), "Name" : "Test2", "Age" : 30, "Gender" : "M", "Class" : "Chemistry", "Score" : 90 }
{ "_id" : ObjectId("5afc076e8dda484c0dacf0cf"), "Name" : "Test3", "Age" : 30, "Gender" : "M", "Class" : "Chemistry", "Score" : 90 }

查询姓名如student1、student2并且性别为男性,以及年龄大于等于25岁的学生:

> db.students.find({"Name":/student*/i,"Age":{"$gte":25},"Gender":"M"})
{ "_id" : ObjectId("5afc076d8dda484c0dacf0cb"), "Name" : "Student1", "Age" : 30, "Gender" : "M", "Class" : "Biology", "Score" : 90 }
{ "_id" : ObjectId("5afc076d8dda484c0dacf0cc"), "Name" : "Student2", "Age" : 30, "Gender" : "M", "Class" : "Chemistry", "Score" : 90 }

3、MapReduce

MapReduce是为了批量方法使用的,而非用于实时分析;

3.1 找出集合中的男同学和女同学的人数:

> var map = function() {emit(this.Gender,1);};
> var reduce = function(key, value) {return Array.sum(value);};
> db.students.mapReduce(map, reduce, {out: "mapreducecount1"})
{
        "result" : "mapreducecount1",
        "timeMillis" : 207,
        "counts" : {
                "input" : 15,
                "emit" : 15,
                "reduce" : 2,
                "output" : 2
        },
        "ok" : 1
}
> db.mapreducecount1.find()
{ "_id" : "F", "value" : 6 }
{ "_id" : "M", "value" : 9 }

3.2 找出按照班级统计的平均分:

> var map_1 = function() {emit(this.Class,this.Score);};
> var reduce_1 = function(key, value) {return Array.avg(value)};
> db.students.mapReduce(map_1, reduce_1, {out:"MR_ClassAvg_1"})
{
        "result" : "MR_ClassAvg_1",
        "timeMillis" : 49,
        "counts" : {
                "input" : 15,
                "emit" : 15,
                "reduce" : 3,
                "output" : 5
        },
        "ok" : 1
}
> db.MR_ClassAvg_1.find()
{ "_id" : "Biology", "value" : 90 }
{ "_id" : "C1", "value" : 85 }
{ "_id" : "C2", "value" : 93 }
{ "_id" : "C3", "value" : 90 }
{ "_id" : "Chemistry", "value" : 90 }

4、aggregate()  聚合框架

4.1 找出集合中的男同学和女同学的人数:

> db.students.aggregate({$group:{_id:"$Gender", totalStudent: {$sum:1}}})
{ "_id" : "F", "totalStudent" : 6 }
{ "_id" : "M", "totalStudent" : 9 }

4.2 找出按照班级统计的平均分:

> db.students.aggregate({$group:{_id:"$Class", AvgScore: {$avg: "$Score"}}})
{ "_id" : "Chemistry", "AvgScore" : 90 }
{ "_id" : "C1", "AvgScore" : 85 }
{ "_id" : "C2", "AvgScore" : 93 }
{ "_id" : "C3", "AvgScore" : 90 }
{ "_id" : "Biology", "AvgScore" : 90 }

猜你喜欢

转载自blog.csdn.net/hellboy0621/article/details/80340731