任务

Mongodb SQL

备注

所有age大于20并且sex为“男”的数据。(隐式)

db.getCollection('example_data_1').find({'age':{'$gt':20},'sex':'男'})

如果SQL无效注意检查下存入的数据是int还是string

所有age大于20并且sex为“男”的数据。(显式)

db.getCollection('example_data_1').find({'$and':[{'age':{'$gt':20}},{'sex':'男'}]})

 

查询所有年龄大于20,性别为“男”,并且id小于10的数据(显式和隐式混用)

db.getCollection('example

_data_1').find({
'id':{'$lt':10},
'$and':[{'age':{'$gt':20}},{'sex':'男'}]
})

所有隐式AND操作都可以改写为显式AND操作。但反之不行,有

一些显式AND操作不能改写为隐式AND操作。

显式OR操作举例

db.getCollection('example

_data_1').find({
'$or':[{'age':{'$gt':28}},
{'salary':{'$gt':9900}}]
})

OR操作一定是显式的,不存在隐式的OR操作

不能写成隐式的AND操作的举例

db.getCollection('example_data_1').find({
'$and':[
{'$or':[{'age':{'$gt':28}},{'salary':{'$gt':9900}}]},
{'$or':[{'sex':'男'},{'id':{'$lt':20}}]}
]
})

使用换行和缩进可以让代码看起来更清晰

易懂

使用点号定位到嵌套字段user中的子字段user_id为102的数据

db.getCollection('example_data_2').find({'user.user_id': 102})

嵌入式文档查询

查询所有“followed”大于10的数据的语句如下

db.getCollection('example_data_2').find({'user.followed': {'$gt': 10}})

嵌入式文档查询

返回嵌套字段中的特定内容(只返回“name”和“user_id”这两个字段)

db.getCollection('example_data_2').find(
{'user.followed':{'$gt':10}},
{'_id':0,'user_name':1,'user.user_id':1}
)

嵌入式文档查询

要查出所有“size”包含“M”的数据,

db.getCollection('example_data_3').find({'size': 'M'})

数组应用

查询所有某个数组不包含某个数据的记录

db.getCollection('example_post2').find({'size': {'$ne': 'M'}})

数组应用

数组中至少有

一个元素在某个范围内。

db.getCollection('example_data_3').find({'price': {'$lt': 300, '$gte':
200}})

数组应用

查询所有“price”字段长度为2的记录

db.getCollection('example_post2').find({'price': {'$size': 2}})

数组应用

查询所有“size”的第1个数据为“S”的记录

db.getCollection('example_post2').find({'size.0': 'S'})

数组应用

查询“price”第1个数据大于500的

所有记录

db.getCollection('example_post2').find({'price.0': {'$gt': 500}})

数组应用

从example_data_1数据集中,查询age大于等于27,且sex

为“女”的所有记录。

db.getCollection('example_data_1').aggregate([{'$match':{'age':{'$gte':27},'sex':'女'}}])

与下方语句等效:

db.getCollection('example_data_1').find({'age': {'$gte': 27}, 'sex':
'女'})

聚合查询

查询所有age大于28或者sex为“男”的记录

db.getCollection('example_data_1').aggregate([
{'$match':{'$or':[{'age':{'$gt':28}},{'sex':'男'}]}}
])

聚合查询

不返回“_id”字段,只返回age和

sex字段

db.getCollection('example_data_1').aggregate([
{'$project':{'_id':0,'sex':1,'age':1}}
])

聚合查询

选择所有age

大于28的记录,只返回age和sex

db.getCollection('example_data_1').aggregate([
{'$match':{'age':{'$gt':28}}},
{'$project':{'_id':0,'sex':1,'age':1}}
])

聚合查询

在“$project”的Value字典中添加一个不存在的字段

db.getCollection('example_data_1').aggregate([
{'$match':{'age':{'$gt':28}}},
{'$project':{'_id':0,'sex':1,'age':1,'hello':'world'}}
])

聚合查询

上面代码中的“world”修改为“$age”,

db.getCollection('example_data_1').aggregate([

{'$match':{'age':{'$gt':28}}},

{'$project':{'_id':0,'sex':1,'age':1,'hello':'$age'}}

])

聚合查询

把原有的age的值改为其他数据

db.getCollection('example_data_1').aggregate([
{'$match':{'age':{'$gt':28}}},
{'$project':{'_id':0,'sex':1,'age':"this is age"}}
])

聚合查询

使用find(),想返回“user_id”和“name”,

db.getCollection('example_data_2').find({}, {'user.name': 1,
'user.user_id': 1})

聚合查询

使用“$project”,则可以把嵌套字段中的内容“抽取”出来,变成普通字段

db.getCollection('example_data_2').aggregate([
{'$project':{'name':'$user.name','user_id':'$user.user_id'}}
])

聚合查询

特殊字段的值normalstring和“$project”的自身语法冲突

db.getCollection('example_data_1').aggregate([
{'$match':{'age':{'$gt':28}}},
{'$project':{'_id':0,'id':1,'hello':{'$literal':'$normalstring'},'abcd':{'$literal':1}}}
])

聚合查询

使用“distinct”函数去重

db.getCollection('example_data_4').distinct('name')

聚合查询

aggregate方式去重

db.getCollection('example_data_4').aggregate([{'$group': {'_id':
'$name'}}])

分组操作虽然也能实现去重操作,但是它返回的数据格式

与“distinct”函数是不一样的。“distinct”函数返回的是数组,而分组操作

返回的是3条记录

分组操作并计算统计值

db.getCollection('example_data_4').aggregate([
{'$group':
{'_id':'$name',
'max_score':{'$max':'$score'},
'min_score':{'$min':'$score'},
'sum_score':{'$sum':'$score'},
'average_score':{'$avg':'$score'}
}
}
])

聚合查询

“$sum”的值还可以使用数字“1”,这样查询语句就变成了统

计每一个分组内有多少条记录

db.getCollection('example_data_4').aggregate([
{'$group':
{'_id':'$name',
'doc_count':{'$sum':1},
'max_score':{'$max':'$score'},
'min_score':{'$min':'$score'},
'sum_score':{'$sum':'$score'},
'average_score':{'$avg':'$score'}
}
}
])

聚合查询

分组操作并去重                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

db.getCollection('example_data_4').aggregate([
{'$group':{'_id':'$name',
'data':{'$last':'$date'},
'score':{'$last':'$score'}
}}
])

 

以name为基准去重,然后取所有字段最老的值

db.getCollection('example_data_4').aggregate([
{'$group':{'_id':'$name',
'data':{'$first':'$date'},
'score':{'$first':'$score'}
}}
])

 

把字段size拆开

db.getCollection('example_post2').aggregate([{'$unwind': '$size'}])

“$unwind”一次只能拆开一个数组

在上面的基础上拆开price字段

db.getCollection('example_post2').aggregate([

{'$unwind': '$size'},

{'$unwind': '$price'}

])

 

在微博集合中查询用户信息,那么主集合就是微博

集合,被查集合就是用户集合。

db.getCollection('example_post').aggregate([
{'$lookup':{
'from':'example_user',
'localField':'user_id',
'foreignField':'id',
'as':'user_info'
}
}
])

 

美化上方的输出结果

db.getCollection('example_post').aggregate([
{'$lookup':{
'from':'example_user',
'localField':'user_id',
'foreignField':'id',
'as':'user_info'
}
},
{'$unwind':'$user_info'}
])

 

联集合查询并拆分结果再返回特定内容

db.getCollection('example_post').aggregate([
{'$lookup':{
'from':'example_user',
'localField':'user_id',
'foreignField':'id',
'as':'user_info'
}
},
{'$unwind':'$user_info'},
{'$project':
{
'content':1,
'post_time':1,
'name':'$user_info.name',
'work':'$user_info.work'}}
])

 

以用户为基准联集合查询

db.getCollection('example_user').aggregate([
{'$lookup':{
'from':'example_post',
'localField':'id',
'foreignField':'user_id',
'as':'weibo_info'
}
}
])

重点学习

以用户为基准联集合查询,再拆分结果,最后输出特定
内容

 

db.getCollection('example_user').aggregate([
{'$lookup':{
'from':'example_post',
'localField':'id',
'foreignField':'user_id',
'as':'weibo_info'
}
},
{'$unwind':'$weibo_info'},
{'$project':{
'name':1,
'work':1,
'content':'$weibo_info.content',
'post_time':'$weibo_info.post_time'}}
])

重点学习

现在只需要查询名为“张小二”的用户发送的微
博(方法一)

db.getCollection('example_user').aggregate([
{'$match':{'name':'张小二'}},
{'$lookup':{
'from':'example_post',
'localField':'id',
'foreignField':'user_id',
'as':'weibo_info'
}
},
{'$unwind':'$weibo_info'},
{'$project':{
'name':1,
'work':1,
'content':'$weibo_info.content',
'post_time':'$weibo_info.post_time'}}
])

重点学习

现在只需要查询名为“张小二”的用户发送的微
博(方法二)

db.getCollection('example_user').aggregate([
{'$lookup':{
'from':'example_post',
'localField':'id',
'foreignField':'user_id',
'as':'weibo_info'
}
},
{'$match':{'name':'张小二'}},
{'$unwind':'$weibo_info'},
{'$project':{
'name':1,
'work':1,
'content':'$weibo_info.content',
'post_time':'$weibo_info.post_time'}}
])

重点学习

如果 MongoDB 的查询语句每一个关键字都使用了引号包起来,那
么这些查询语句直接复制到Python中就可以使用。

 

_id:0的作用:

The find() method always returns the _id field unless you specify _id0 to suppress the field.