时间:2021-07-01 10:21:17 帮助过:20人阅读
CountDistinctGroup 1.Count 请查询persons中美国学生的人数. db.persons.find({country:USA}).count() 2.Distinct 请查询出persons中一共有多少个国家分别是什么. db.runCommand({distinct:persons“, key:country}).values 3.Group 语法: db.runCommand({g
Count+Distinct+Group
1.Count
请查询persons中美国学生的人数.
db.persons.find({country:"USA"}).count()
2.Distinct
请查询出persons中一共有多少个国家分别是什么.
db.runCommand({distinct:"persons“, key:"country"}).values
3.Group
语法:
db.runCommand({group:{
ns:集合名字,
Key:分组的键对象,
Initial:初始化累加器,
$reduce:组分解器,
Condition:条件,
Finalize:组完成器
}})
分组首先会按照key进行分组,每组的 每一个文档全要执行$reduce的方法,
他接收2个参数一个是组内本条记录,一个是累加器数据.
3.1请查出persons中每个国家学生数学成绩最好的学生信息(必须在90以上)
db.runCommand({group:{
ns:"persons",
key:{"country":true},
initial:{m:0},
$reduce:function(doc,prev){
if(doc.m > prev.m){
prev.m = doc.m;
prev.name = doc.name;
prev.country = doc.country;
}
},
condition:{m:{$gt:90}}
}})
3.2在3.1要求基础之上把每个人的信息链接起来写一个描述赋值到m上
finalize:function(prev){
prev.m = prev.name+" Math scores "+prev.m
}
4.用函数格式化分组的键
4.1如果集合中出现键Counrty和counTry同时存在
db.persons.insert({
name:"USPCAT",
age:27,
email:"2145567457@qq.com",
c:89,m:100,e:67,
counTry:"China",
books:["JS","JAVA","EXTJS","MONGODB"]
})
那分组有点麻烦这要如何解决呢?
db.runCommand({group:{
ns:"persons",
$keyf:function(doc){
if(doc.counTry){
return {country:doc.counTry}
}else{
return {country:doc.country}
}
},
initial:{m:0},
$reduce:function(doc,prev){
if(doc.m > prev.m){
prev.m = doc.m;
prev.name = doc.name;
if(doc.country){
prev.country = doc.country;
}else{
prev.country = doc.counTry;
}
}
},
finalize:function(prev){
prev.m = prev.name+" Math scores "+prev.m
},
condition:{m:{$gt:90}}
}})
数据库命令操作
1.命令执行器runCommand
1.1用命令执行完成一次删除表的操作
db.runCommand({drop:"map"})
{
"nIndexesWas" : 2,
"msg" : "indexes dropped forcollection",
"ns" : "foobar.map",
"ok" : 1
}
2.如何查询mongoDB为我们提供的命令
1.在shell中执行 db.listCommands()
2.访问网址http://localhost:28017/_commands,需要先通过
mongod --dbpath d:\app\mongodata --rest命令打开简单的rest API
3.常用命令举例
3.1查询服务器版本号和主机操作系统
db.runCommand({buildInfo:1})
3.2查询执行集合的详细信息,大小,空间,索引等……
db.runCommand({collStats:"persons"})
3.3查看操作本集合最后一次错误信息
db.runCommand({getLastError:"persons"})
固定集合特性
2.固定特性
2.1固定集合默认是没有索引的就算是_id也是没有索引的
2.2由于不需分配新的空间他的插入速度是非常快的
2.3固定集合的顺是确定的导致查询速度是非常快的
2.4最适合的是应用就是日志管理<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+My60tL2oucy2qLyvus88L3A+CjxwPiAgICAgICAzLjG0tL2o0ru49tDCtcS5zLaovK+6z9Kqx/O089ChyscxMDC49tfWvdosv8nS1LTmtKLOxLW1MTC49jwvcD4KPHA+ICAgICAgICAgICAgZGIuY3JlYXRlQ29sbGVjdGlvbigmcXVvdDtteWNvbGwmcXVvdDsse3NpemU6MTAwLGNhcHBlZDp0cnVlLG1heDoxMH0pPC9wPgo8cD4gICAgICAgIDMuMrDR0ru49sbVzai8r7rP16q7u7PJucy2qLyvus88L3A+CjxwPiAgICAgICAgICAgICBkYi5ydW5Db21tYW5kKHtjb252ZXJ0VG9DYXBwZWQ6obFwZXJzb25zobEsc2l6ZToxMDAwMDB9KTwvcD4KPHA+NC63tM/yxcXQ8izErMjPysey5cjry7PQ8sXF0PIuPC9wPgo8cD4gICAgICAgIDQuMbLp0a+5zLaovK+6z215Y29sbLKix9K3tM/sxcXQ8jwvcD4KPHA+ICAgICAgICAgICAgIGRiLm15Y29sbC5maW5kKCkuc29ydCh7JG5hdHVyYWw6LTF9KTwvcD4KPHA+NS7OsrK/086x6iy/yc+nc2hlbGyyu9ans9ZqYXZ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xlcyAtZCBmb29iYXIgZ2V0ICZxdW90O2EudHh0obA8L3A+CjxwPiAgICBWVUW/ydLUsum/tCxzaGVsbM7et6i08r+qzsS8/jwvcD4KPHA+My41sum/tMv509DOxLz+PC9wPgo8cD4gICAgbW9uZ29maWxlcyAtZCBmb29iYXIgbGlzdDwvcD4KPHA+My41yb6z/dLRvq205tTatcTOxLz+VlVF1tCy2df3PC9wPgo8cD4gICAgbW9uZ29maWxlcyAtZCBmb29iYXIgZGVsZXRlIA=="a.txt'
服务器端脚本
1.Eval
1.1服务器端运行eval
db.eval("function(name){return name}","uspcat")
2.Javascript的存储
2.1在服务上保存js变量活着函数共全局调用
1.把变量加载到特殊集合system.js中
db.system.js.insert({_id:name,value:”uspcat”})
2.调用
db.eval("return name;")
System.js相当于Oracle中的存储过程,因为value不单单可以写变量
还可以写函数体也就是javascript代码