1 格式 BSON
SON是⼀种类json的⼀种⼆进制形式的存储格式,简称Binary JSON,它和JSON⼀样,⽀持内嵌的⽂档对象和数组对象,但是BSON有JSON没有的⼀些数据类型,如Date和Binary Data类型。BSON可以做为⽹络数据交换的⼀
种存储形式,是⼀种schema-less的存储形式,它的优点是灵活性⾼,但它的缺点是空间利⽤率不是很理想。{key:value,key2:value2} 这是⼀个BSON的例⼦,其中key是字符串类型,后⾯的value值,它的类型⼀般是字符串,double,Array,ISODate等类型。BSON有三个特点:轻量性、可遍历性、⾼效性
2 基本语法
show dbs
//创建库
use lg_resume
//创建集合
db.createCollection(“lg_resume_preview”)
//删除集合
db.lg_resume_preview.drop()
show collections
//插入数据
db.lg_resume_preview.insert([
{name:“张三”,birthday:new ISODate(“1992-03-20”),expectsalary:15000,city:“长沙”},
{name:“李四”,birthday:new ISODate(“1995-07-25”),expectsalary:12000,city:“长沙”},
{name:“王五”,birthday:new ISODate(“1998-03-20”),expectsalary:15000,city:“南京”},
{name:“赵六”,birthday:new ISODate(“1995-07-25”),expectsalary:12000,city:“上海”}
])
db.lg_resume_preview.insert({name:“张晓峰”,birthday:new ISODate(“2000-07-01”),gender:1,expectSalary:15000,city:“bj”})
db.lg_resume_preview.insert({name:“张震”,birthday:new ISODate(“1995-04-01”),gender:1,expectSalary:18000,city:“bj”})
db.lg_resume_preview.insert({name:“李山”,birthday:new Date(“1995-04-01”),gender:1,expectSalary:25000,city:“sh”})
db.lg_resume_preview.insert({name:“李笑来”,birthday:new Date(“1998-04-01 14:20:09”),gender:1,expectSalary:20000,city:“sh”})
db.lg_resume_preview.insert({name:null,birthday:new Date(“1992-04-01 14:20:09”),gender:1,expectSalary:30000,city:“sh”})
db.lg_resume_preview.insert({birthday:new Date(“1991-05-01 14:20:09”),gender:1,expectSalary:50000,city:“sz”})
db.lg_resume_preview.insert(
[{name:“李丽”,birthday:new Date(“1996-05-01 14:20:09”),gender:0,expectSalary:21000,city:“sz”},
{name:“李平”,birthday:new Date(“1997-07-01 14:20:09”),gender:0,expectSalary:22000,city:“sz”}])
//查询数据
db.lg_resume_preview.find({name:{KaTeX parse error: Expected 'EOF', got '}' at position 9: eq: "张三"}̲})//等值查询 db.lg_…gt: 15000}})//大于查询
db.lg_resume_preview.find({expectSalary:{KaTeX parse error: Expected 'EOF', got '}' at position 11: gte: 15000}̲})//大于等于查询 db.l…lt: 15000}})//小于
db.lg_resume_preview.find({expectSalary:{KaTeX parse error: Expected 'EOF', got '}' at position 11: lte: 15000}̲})//小于等于 db.lg_…ne:15000}})//不等于
db.lg_resume_preview.find({city:“bj”,expectSalary:15000})//and条件
db.lg_resume_preview.find({KaTeX parse error: Expected 'EOF', got '}' at position 39: …tSalary:15000}]}̲)//and条件 db.get…or:[{name:“李四”},{expectSalary:25000}]})//or 条件
db.lg_resume_preview.find({city:{KaTeX parse error: Expected '}', got 'EOF' at end of input: not:{eq:“bj”}}})//非
db.lg_resume_preview.find().sort({expectSalary:1}).limit(3)//分页排序
/**
更新操作
upsert:true,表示没有就新增,默认false
multi:表示只更新一条,默认false
/
db.lg_resume_preview.find({name:“张震”})
db.lg_resume_preview.update({name:“张震”},
{KaTeX parse error: Expected 'EOF', got '}' at position 25: …ctSalary:28000}}̲, { multi:…inc: {expectSalary:-10000}},
{ multi: false, upsert: false}
)
/
删除 justOne:表示只删除一条数据
默认为 false
/
db.lg_resume_preview.find({city:“bj”})
db.lg_resume_preview.remove({city:“bj”}, {justOne: true})
db.lg_resume_preview.remove({}, {justOne: false})
/
聚合函数
/
//单目聚合
db.lg_resume_preview.count()//统计个数
db.lg_resume_preview.find({city:“sh”}).count()//统计个数
db.lg_resume_preview.distinct(“city”)//按照城市去重
//聚合管道
/
$group:将集合中的⽂档分组,可⽤于统计结果。
$project:修改输⼊⽂档的结构。可以⽤来重命名、增加或删除域,也可以⽤于创建计算结果以及嵌套⽂档。
m
a
t
c
h
:⽤于过滤数据,只输出符合条件的⽂档。
match:⽤于过滤数据,只输出符合条件的⽂档。
match:⽤于过滤数据,只输出符合条件的⽂档。match使⽤MongoDB的标准查询操作。
$limit:⽤来限制MongoDB聚合管道返回的⽂档数。
$skip:在聚合管道中跳过指定数量的⽂档,并返回余下的⽂档。
$sort:将输⼊⽂档排序后输出。
KaTeX parse error: Expected '}', got 'EOF' at end of input: …ew.aggregate([{group: { _id: “KaTeX parse error: Expected '}', got 'EOF' at end of input: …y",city_count:{sum: 1}}}])
/** 按照city进行分组,统计每个city中expectSalary的平均值**/
db.lg_resume_preview.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group: { _id: "city”,avg_sal:{
a
v
g
:
"
avg: "
avg:"expectSalary"}}}])
/** 按照city进行分组,统计每个city的值放入一个数组中**/
db.lg_resume_preview.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group: { _id: "city",city_name:{
p
u
s
h
:
"
push: "
push:"city"}}}])//重复的也加入到数组中
db.lg_resume_preview.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group: { _id: "city",city_name:{
a
d
d
T
o
S
e
t
:
"
addToSet: "
addToSet:"city"}}}])//计入set数组去掉重复的
db.lg_resume_preview.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group: { _id: "city",city_count:{KaTeX parse error: Expected 'EOF', got '}' at position 7: sum: 1}̲}},{project: {city:“
c
i
t
y
"
,
c
o
u
n
t
:
"
city",count:"
city",count:"city_count”}}])//重新命名
db.lg_resume_preview.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: group: { _id: "city",count:{KaTeX parse error: Expected 'EOF', got '}' at position 6: sum:1}̲}},{match: {count:{$gt: 2}}}])//分组后,查询城市大于2的数据
/**
使⽤ MapReduce 要实现两个函数 Map 函数和 Reduce
函数,Map 函数调⽤ emit(key, value), 遍历 collection 中所
有的记录, 将 key 与 value 传递给 Reduce 函数进⾏处理
map:是JavaScript 函数,负责将每⼀个输⼊⽂档转换为零或多个⽂档,⽣成键值对序列,作为 reduce 函数参
数
reduce:是JavaScript 函数,对map操作的输出做合并的化简的操作(将key-value变成key-values,也就是
把values数组变成⼀个单⼀的值value)
out:统计结果存放集合
query: ⼀个筛选条件,只有满⾜条件的⽂档才会调⽤map函数。
sort: 和limit结合的sort排序参数(也是在发往map函数前给⽂档排序),可以优化分组机制
limit: 发往map函数的⽂档数量的上限(要是没有limit,单独使⽤sort的⽤处不⼤)
finalize:可以对reduce输出结果再⼀次修改
verbose:是否包括结果信息中的时间信息,默认为fasle
/
db.lg_resume_preview.mapReduce(
function(){emit(this.city,this.expectSalary);},
function(key,value){return Array.avg(value)},
{
query:{expectSalary:{$gt:15000}},
out:“cityAvgSal”,
finalize:function(key,value){
return value+5000
}
}
)
/
索引
/
/ 单键索引 db.集合名.createIndex({“字段名”:排序⽅式}) */
db.lg_resume_preview.getIndexes();
db.lg_resume_preview.createIndex({name:1})
/ 过期索引 特殊的单键索引 过期索引 TTL
TTL索引是MongoDB中⼀种特殊的索引, 可以⽀持⽂档在⼀定时间之后⾃动过期删除,⽬前TTL索引只能在单字
段上建⽴,并且字段类型必须是⽇期类型
db.集合名.createIndex({“⽇期字段”:排序⽅式}, {expireAfterSeconds: 秒数})
***/
db.createCollection(“lg_index_test”)
db.lg_index_test.insert([{name:“张三”,salary:18000,birth_day:new ISODate(“1992-09-01”)}
,{name:“李四”,salary:12000}
]);
db.lg_index_test.getIndexes();
db.lg_index_test.createIndex({birth_day:1},{expireAfterSeconds:10});
/**复合索引
db.集合名.createIndex( { “字段名1” : 排序⽅式, “字段名2” : 排序⽅式 } )
制作复合索引时要注意的重要事项包括:字段顺序与索引⽅向。
*/
db.lg_index_test.createIndex({name:1,salary:-1});
db.lg_index_test.find({salary:12000})//此时没走复合索引,
db.lg_index_test.find({name:“李四”,salary:12000})//索引支持对 name 字段以及 name 和 salary 字段的查询:
db.lg_index_test.find().sort({name:1,salary:-1})//支持复合索引
db.lg_index_test.find().sort({name:-1,salary:1})//支持复合索引
db.lg_index_test.find().sort({name:1,salary:-1})//不支持复合索引
/
多键索引
针对属性包含数组数据的情况,MongoDB⽀持针对数组中每⼀个element创建索引,Multikey indexes⽀持
strings,numbers和nested documents
/
/
地理空间索引(Geospatial Index)
**/
db.company.insert(
{
loc : { type: “Point”, coordinates: [ 116.482451, 39.914176 ] },
name: “大望路地铁”,
category : “Parks”
}
)
db.company.insert(
{
loc : { type: “Point”, coordinates: [ 116.492451, 39.934176 ] },
name: “test1”,
category : “Parks”
}
)
db.company.insert(
{
loc : { type: “Point”, coordinates: [ 116.462451, 39.954176 ] },
name: “test2”,
category : “Parks”
}
)
db.company.insert(
{
loc : { type: “Point”, coordinates: [ 116.562451, 38.954176 ] },
name: “test3”,
category : “Parks”
}
)
db.company.insert(
{
loc : { type: “Point”, coordinates: [ 117.562451, 37.954176 ] },
name: “test4”,
category : “Parks”
}
)
/**
2dsphere索引,⽤于存储和查找球⾯上的点
2d索引,⽤于存储和查找平⾯上的点
/
db.company.createIndex({ loc : “2dsphere” })
/ 查询离大望路地铁五公里内的数据*/
db.company.find(
{
loc:{
“KaTeX parse error: Expected '}', got 'EOF' at end of input: … "center”: [ [116.482451, 39.914176 ] , 0.05 ]
}
}
}
);
/** 计算中心点最近的三个点 dist.calculated 分布计算*/
db.company.aggregate([
{
$geoNear:{
near:{ type: “Point”, coordinates: [116.482451,39.914176 ] },
key:“loc”,
distanceField: “dist.calculated”
}
},{
KaTeX parse error: Expected 'EOF', got '}' at position 14: limit: 3 }̲ ]) /** 全文索…text": { "KaTeX parse error: Expected 'EOF', got '}' at position 19: …rch": "dream" }̲}); /** 索引管理 …gt:“test203330”}}).explain(“executionStats”);
db.lg_resume.find({id:{KaTeX parse error: Expected 'EOF', got '}' at position 10: gt:203330}̲}).explain("exe…gt:“test203330”}},{_id:0}).explain(“executionStats”);
/*** 慢查询日志查看 ms **/
db.setProfilingLevel(1,450);
db.lg_resume.find({name:“test9999”})
db.lg_resume.find({name:“test9998”})
db.lg_resume.find({name:“test9996”})
db.lg_resume.find({name:“test9997”})
db.lg_resume.find({name:“test9995”})
db.lg_resume.find({id:11111})
db.lg_resume.find({id:11112})
//查看最慢的几条sql
db.system.profile.find().sort({millis:-1}).limit(3)
db.lg_resume_datas.find({name:“lisi”});
db.lg_resume_datas.find();