比较运算符
⼩于:$lt (less than)
⼩于等于:$lte (less than equal)
⼤于:$gt (greater than)
⼤于等于:$gte
不等于:$ne
db.stu.insert([{“name” : “郭靖”, “hometown” : “蒙古”, “age” : 20, “gender” : true },{“name” : “⻩蓉”, “hometown” : “桃花岛”, “age” : 18, “gender” : false },{“name” : “华筝”, “hometown” : “蒙古”, “age” : 18, “gender” : false },{“name” : “⻩药师”, “hometown” : “桃花岛”, “age” : 40, “gender” : true },{“name” : “段誉”, “hometown” : “⼤理”, “age” : 16, “gender” : true },{“name” : “段王爷”, “hometown” : “⼤理”, “age” : 45, “gender” : true },{“name” : “洪七公”, “hometown” : “华⼭”, “age” : 18, “gender” : true }])
逻辑运算符
- and :{name:"",age:""} 此时name和age表示and的逻辑
- or : {$or:[{条件1},{条件2}]} 条件1和2之前属于or关系
范围运算符
$in :在范围内 {age:{$in:[19,20,30]}}
- $nin :不在范围内
正则
db.products.insert([{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." },{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" },{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before line" },{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }])
投影
指定数据内容的字段
db.stu.find({条件},{name:1,_id:0})
返回数据中只会包含name字段,_id
不会显示
mongodb的分组
- 分组
db.stu.aggregate(
{$group:{_id:"$hometown",count:{$sum:1},total_age:{$sum:"$age"},avg_age:{$avg:"$age"}}}
)
-
_id
分组的依据 -
$age
取age对应的值 -
$sum:1
把每条数据作为1进行统计,统计的是个数 -
$sum:"$age"
统计年龄对应的和 -
$group
对应的字典中的键是输出数据的键 -
不分组
db.stu.aggregate(
{$group:{_id:null,count:{$sum:1}}}
)
统计整个文档
- 数据透视
把不同行的数据,放到一行来展示
db.stu.aggregate(
{$group:{_id:"$gender",name:{$push:"$name"},hometown:{$push:"$hometown"}}}
)
- 按照过个字段进行分组
按照多个字端进行分组,_id
的值是一个json
db.stu.aggregate(
{$group:{_id:{hometown:"$hometown",gender:"$gender"},count:{$sum:1}}}
)
多字段分组练习
当某个键对应的值是字典的时候,取其中的值需要使用.操作,$_id.country
表示取到_id
这个字典下的country的键对应的值
db.tv3.aggregate(
{$group:{_id:{country:"$country",province:"$province",userid:"$userid"}}},
{$group:{_id:{country:"$_id.country",province:"$_id.province"},count:{$sum:1}}}
)
$project的使用
db.stu.aggregate(
{$group:{_id:"$hometown",count:{$sum:1}}},
{$project:{_id:0,sum:"$count",hometown:"$_id"}}
)
db.tv3.aggregate(
{$group:{_id:{country:"$country",province:"$province",userid:"$userid"}}},
{$group:{_id:{country:"$_id.country",province:"$_id.province"},count:{$sum:1}}},
{$project:{country:"$_id.country",province:"$_id.province",counter:"$count",_id:0}}
)
$skip $limit $sort
db.stu.aggregate(
{$group:{_id:"$hometown",count:{$sum:1}}},
{$sort:{count:-1}},
{$skip:1},
{$limit:2}
)
douban数据练习
db.tv1.aggregate(
{$project:{title:1,_id:0,count:"$rating.count",rate:"$rating.value",country:"$tv_category"}},
{$match:{rate:{$gte:8}}},
{$group:{_id:"$country",count:{$sum:1}}}
)
abc abd a
a abc abd bcde
db.stu.insert([{ “country” : “china”, “province” : “sh”, “userid” : “a” },{ “country” : “china”, “province” : “sh”, “userid” : “b” },{ “country” : “china”, “province” : “sh”, “userid” : “a” },{ “country” : “china”, “province” : “sh”, “userid” : “c” },{ “country” : “china”, “province” : “bj”, “userid” : “da” },{“country” : “china”, “province” : “bj”, “userid” : “fa” }])
db.stu1.aggregate({KaTeX parse error: Expected '}', got 'EOF' at end of input: …{_id:{country:'country’,province:‘ p r o v i n c e ′ , u s e r i d : ′ province',userid:' province′,userid:′userid’}}})