MongoDB的高級語法

 

 任務 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. 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章