mysql | MongoDB | |
---|---|---|
inert into test value(3,5) | db.test.insert({c1:3, c2:5}) db.test.insert({c1:36, c2:56, “name”:”abcd”}) | |
select count(1) from test | db.test.count() | |
select * from test | db.test.find() | |
select c1 from test | db.test.find({},{“c1”:true}) | |
select * from test where c1=3 | db.test.find({“c1”:3}) | |
select * from test where c1=3 order by c2 | db.test.find({“c1”:3}).sort({“c2”:1}) | |
select * from test where c1>3 order by c2 | db.test.find({"c1":{"$gt":3}}) | 条件操作符,"$lt", "$lte", "$gt", "$gte", "$ne"就是全部的比较操作符,对应于"<", "<=", ">", ">=","!=" |
select * from test where name like "%jjj%" | db.test.find({"name":/c/}) | |
select * from test where name like "a%" | db.test.find({"name":/^a/}) | |
select * from test where c1>2 and c1 < 4 | db.test.find({"c1":{"$gt":2,"$lt":4}}) | |
select * from test where c1>2 or c2 < 4 | db.test.find({"$or":[{c1:{"$gt":1}}, {"c2":{"$lt":4}}]}) | |
select * from test limit 1 | db.test.find().limit(1) db.test.findOne() | |
select * from test limit 1,1 | db.test.find().limit(1).skip(1) | |
select distinct c1 from test | db.test.distinct("c1") | |
select count(name) from test | db.test.find({"name":{"$exists":true}}).count() | |
CREATE INDEX name_idx ON test(name); | db.test.createIndex({"name":1}) | |
explain select * from test | db.test.find().explain() |
测试数据
> db.job.find()
{ "_id" : ObjectId("57274ee71d41c83d34573438"), "co" : "北京车到网络科技有限公司", "ad" : "北京", "title" : "高级JAVA工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d34573439"), "co" : "杭州蜂贷网络科技有限公司", "ad" : "杭州", "title" : "java开发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d3457343a"), "co" : "金仓数据库(上海)有限公司", "ad" : "北京", "title" : "java软件工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d34573459"), "co" : "浙江菁英电商管理服务有限公司", "ad" : "金华", "title" : "java开发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d3457345b"), "co" : "桂林赛普电子科技有限公司", "ad" : "异地招聘", "title" : "JAVA高级工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d3457345d"), "co" : "上海柏维信息技术有限公司", "ad" : "上海", "title" : "高级java工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d34573464"), "co" : "上海云虎软件有限公司", "ad" : "上海", "title" : "资深java开发(偏前端)", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee71d41c83d34573465"), "co" : "江苏云联科技有限公司", "ad" : "苏州", "title" : "Java开发项目经理", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d34573466"), "co" : "北京依蓝天创科技有限公司", "ad" : "北京", "title" : "JAVA软件开发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d34573469"), "co" : "广州上善信息科技有限公司", "ad" : "广州", "title" : "Java开发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d3457346a"), "co" : "广东三合科技投资有限公司", "ad" : "广州", "title" : "JAVA后台开发", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d3457346b"), "co" : "博易智软(北京)技术股份有限公司...", "ad" : "上海", "title" : "高级java工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d3457346c"), "co" : "北京便联生活科技有限公司", "ad" : "北京", "title" : "Java初级工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d3457346d"), "co" : "北京晨星启明科技有限公司", "ad" : "北京", "title" : "高级JAVA开发工程师-宝大夫", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d3457346e"), "co" : "核工业计算机应用研究所", "ad" : "北京", "title" : "JAVA研发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d3457346f"), "co" : "湖南盛世通和科技有限公司", "ad" : "长沙", "title" : "JAVA工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d34573470"), "co" : "上海通创信息技术有限公司", "ad" : "上海", "title" : "高级Java研发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d34573471"), "co" : "苏州优爱妈妈信息科技有限公司", "ad" : "苏州", "title" : "JAVA开发工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d345734c4"), "co" : "广东知好乐教育科技有限公司", "ad" : "异地招聘", "title" : "java高级工程师", "t" : "java", "type" : 51 }
{ "_id" : ObjectId("57274ee91d41c83d345734c5"), "co" : "广州市时代财富科技有限公司", "ad" : "广州", "title" : "java程序员", "t" : "java", "type" : 51 }
Type "it" for more
group
db.job.group(
{
"key": {"t":true},
"initial":{"num":0},
"$reduce": function(doc,prev){
prev.num++
}
}
)
[
{
"t" : "java",
"num" : 56790
},
{
"t" : "php",
"num" : 19799
},
{
"t" : "python",
"num" : 1245
},
{
"t" : "net",
"num" : 17060
}
]
“key”:表示集合数据分组的依据。这里我们指定了“t”键,那么会根据集合文档中的
t
进行分组。true是指在分组的结果中就会显示每组“t”的键值。
“initial”:初始化变量,num累加器
这个文档中存放是的在分组过程中收集、计算出的信息,不一定是集合文档中的原信息,在后续的$reduce
每个分组都保留当前分组的num用作累加
“$reduce”: 分组过程,“doc”代表分组开发过程 中的每一个集合中的文档,而“prev”则代表“累加器文档(num)”的累加状态
过滤 condition
#只统计上海的
db.job.group(
{
"key": {"t":true},
"initial":{"num":0},
"$reduce": function(doc,prev){
prev.num++
},
"condition": {
"$where": function(){
return this.ad=="上海"
}
}
}
)
文档执行完 finalize
db.job.group(
{
"key": {"t":true},
"initial":{"num":0},
"$reduce": function(doc,prev){
prev.num++
},
"condition": {
"$where": function(){
return this.ad=="上海"
}
},
"finalize": function(prev){
if(prev.t=="java"){
delete prev.t
}
}
}
)
每个城市每个语言的人数
db.job.group( { "key": {"ad":true}, "initial":{"tags": {}}, "$reduce": function(doc,prev){ if(doc.t in prev.tags){prev.tags[doc.t]++}else{prev.tags[doc.t] = 1}}})