案例原始数据为:
db.stu.find()
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ea”), “name” : “郭靖”, “hometown” : “蒙古”, “age” : 20, “gender” : true }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0eb”), “name” : “黄蓉”, “hometown” : “桃花岛”, “age” : 18, “gender” : false }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ec”), “name” : “华筝”, “hometown” : “蒙古”, “age” : 18, “gender” : false }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ed”), “name” : “黄药师”, “hometown” : “桃花岛”, “age” : 40, “gender” : true }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ee”), “name” : “段誉”, “hometown” : “大理”, “age” : 16, “gender” : true }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ef”), “name” : “段王爷”, “hometown” : “大理”, “age” : 45, “gender” : true }
{ “_id” : ObjectId(“5e9114e1217a4bc35b05b0f0”), “name” : “洪七公”, “hometown” : “华山”, “age” : 18, “gender” : true }
按照name进行分组,并输出age的平均值
db.stu.aggregate(
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup:{ _id:'name’,
counter:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲, avgage:{avg:’$age’}
}
}
)
db.stu.aggregate(
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: … _id:'name’,
counter:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲, a…avg:’$age’}
}
}
)
结果:
db.stu.aggregate(
… {
… KaTeX parse error: Expected '}', got 'EOF' at end of input: … _id:'name’,
… counter:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲, ... …avg:’$age’}
… }
… }
… )
{ “_id” : “洪七公”, “counter” : 1, “avgAge” : 18 }
{ “_id” : “段王爷”, “counter” : 1, “avgAge” : 45 }
{ “_id” : “段誉”, “counter” : 1, “avgAge” : 16 }
{ “_id” : “华筝”, “counter” : 1, “avgAge” : 18 }
{ “_id” : “黄药师”, “counter” : 1, “avgAge” : 40 }
{ “_id” : “郭靖”, “counter” : 1, “avgAge” : 20 }
{ “_id” : “黄蓉”, “counter” : 1, “avgAge” : 18 }
安装gender进行分组,结果集中显示符合分组的name
db.stu.aggregate(
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: …p: { _id:'gender’,
name:{ p u s h : ′ push:' push:′name’}
}
}
)
结果:
db.stu.aggregate(
… {
… KaTeX parse error: Expected '}', got 'EOF' at end of input: …... { ... _id:'gender’,
… name:{ p u s h : ′ push:' push:′name’}
… }
… }
… )
{ “_id” : false, “name” : [ “黄蓉”, “华筝” ] }
{ “_id” : true, “name” : [ “郭靖”, “黄药师”, “段誉”, “段王爷”, “洪七公” ] }
在age<20的结果中按照gender进行分组,并显示每个组的数量
db.stu.aggregate(
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: …h: { age:{lt:20},
}
},
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: …up: { _id:'gender’,
counter:{$sum:1}
}
}
)
结果:
db.stu.aggregate(
… {
… KaTeX parse error: Expected '}', got 'EOF' at end of input: …... { ... _id:'gender’,
… counter:{$sum:1}
… }
… },
… {
… $project:
… {
… _id:0,
… counter:1
… }
… }
… )
{ “counter” : 2 }
{ “counter” : 5 }
显示指定属性
db.stu.aggregate(
{
$project:
{
_id:0,
name:1,
gender:1
}
}
)
结果:
db.stu.aggregate(
… {
… $project:
… {
… _id:0,
… name:1,
… gender:1
… }
… }
… )
{ “name” : “郭靖”, “gender” : true }
{ “name” : “黄蓉”, “gender” : false }
{ “name” : “华筝”, “gender” : false }
{ “name” : “黄药师”, “gender” : true }
{ “name” : “段誉”, “gender” : true }
{ “name” : “段王爷”, “gender” : true }
{ “name” : “洪七公”, “gender” : true }
只输出按照gender分组后的数量
db.stu.aggregate(
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: …up: { _id:'gender’,
counter:{$sum:1}
}
},
{
$project:
{
_id:0,
counter:1
}
}
)
结果:
db.stu.aggregate(
… {
… KaTeX parse error: Expected '}', got 'EOF' at end of input: …... { ... _id:'gender’,
… counter:{$sum:1}
… }
… },
… {
… $project:
… {
… _id:0,
… counter:1
… }
… }
… )
{ “counter” : 2 }
{ “counter” : 5 }
指定排序后按照指定属性进行排序(-1:降序,1:升序)
db.stu.aggregate(
{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup: { _id:'gender’,
counter:{$sum:1}
}
},
{
$sort:
{
counter:1
}
}
)
结果:
db.stu.aggregate(
… {KaTeX parse error: Expected '}', got 'EOF' at end of input: …... { ... _id:'gender’,
… counter:{$sum:1}
… }
… },
… {
… $sort:
… {
… counter:1
… }
… }
… )
{ “_id” : false, “counter” : 2 }
{ “_id” : true, “counter” : 5 }
返回个数限制
db.stu.aggregate({$limit:2})
结果:
db.stu.aggregate({$limit:2})
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ea”), “name” : “郭靖”, “hometown” : “蒙古”, “age” : 20, “gender” : true }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0eb”), “name” : “黄蓉”, “hometown” : “桃花岛”, “age” : 18, “gender” : false }
跳过前多少个
db.stu.aggregate({$skip:2})
结果:
db.stu.aggregate({$skip:2})
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ec”), “name” : “华筝”, “hometown” : “蒙古”, “age” : 18, “gender” : false }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ed”), “name” : “黄药师”, “hometown” : “桃花岛”, “age” : 40, “gender” : true }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ee”), “name” : “段誉”, “hometown” : “大理”, “age” : 16, “gender” : true }
{ “_id” : ObjectId(“5e9114e0217a4bc35b05b0ef”), “name” : “段王爷”, “hometown” : “大理”, “age” : 45, “gender” : true }
{ “_id” : ObjectId(“5e9114e1217a4bc35b05b0f0”), “name” : “洪七公”, “hometown” : “华山”, “age” : 18, “gender” : true }
统计男生、女生人数,按人数升序,取第二条数据
db.stu.aggregate(
{KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:'gender’,counter:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲}}, {project:{counter:1,_id:0}},
{KaTeX parse error: Expected 'EOF', got '}' at position 17: …ort:{counter:1}}̲, {skip:1}
)
结果:
db.stu.aggregate(
… {KaTeX parse error: Expected '}', got 'EOF' at end of input: group:{_id:'gender’,counter:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲}}, ... {project:{counter:1,_id:0}},
… {KaTeX parse error: Expected 'EOF', got '}' at position 17: …ort:{counter:1}}̲, ... {skip:1}
… )
{ “counter” : 5 }
unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值
db.t2.insert({_id:1,item:‘t-shirt’,size:[‘S’,‘M’,‘L’]})
db.t2.aggregate({ u n w i n d : ′ unwind:' unwind:′size’})
结果:
db.t2.aggregate({ u n w i n d : ′ unwind:' unwind:′size’})
{ “_id” : 1, “item” : “t-shirt”, “size” : “S” }
{ “_id” : 1, “item” : “t-shirt”, “size” : “M” }
{ “_id” : 1, “item” : “t-shirt”, “size” : “L” }
unwind的参数设置:属性preserveNullAndEmptyArrays值为false表示丢弃属性值为空的文档,属性preserveNullAndEmptyArrays值为true表示保留属性值为空的文档
db.t3.aggregate(
{
KaTeX parse error: Expected '}', got 'EOF' at end of input: …d: { path:'size’,
preserveNullAndEmptyArrays:false
}
}
)
结果:
db.t3.aggregate(
… {
… KaTeX parse error: Expected '}', got 'EOF' at end of input: ….. { ... path:'size’,
… preserveNullAndEmptyArrays:false
… }
… }
… )
{ “_id” : 1, “item” : “a”, “size” : “S” }
{ “_id” : 1, “item” : “a”, “size” : “M” }
{ “_id” : 1, “item” : “a”, “size” : “L” }
{ “_id” : 3, “item” : “c”, “size” : “M” }
随着数据量的增大,我们需要给部分属性增加索引,加快检索速度。
加索引: db.t1.ensureIndex({name:1})
创建联合索引:db.t1.ensureIndex({name:1,age:1})
创建唯一索引:db.t1.ensureIndex({name:1},{unique:true})
获取全部索引:db.t1.getIndex
删除索引:db.t1.dropIndex(‘索引名称’)
备份
mongodump -h 服务器地址 -d 数据库名称 -o 备份文件路径
回复
mongorestore -h dbhost -d dbname --dir dbdirector