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({ | 所有隱式AND操作都可以改寫為顯式AND操作。但反之不行,有 一些顯式AND操作不能改寫為隱式AND操作。 |
| 顯式OR操作舉例 | db.getCollection('example _data_1').find({ | OR操作一定是顯式的,不存在隱式的OR操作 |
| 不能寫成隱式的AND操作的舉例 | db.getCollection('example_data_1').find({ | 使用換行和縮進可以讓代碼看起來更清晰 易懂 |
| 使用點號定位到嵌套字段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( | 嵌入式文檔查詢 |
| 要查出所有“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': | 數組應用 |
| 查詢所有“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([ | 聚合查詢 |
| 不返回“_id”字段,只返回age和 sex字段 | db.getCollection('example_data_1').aggregate([ | 聚合查詢 |
| 選擇所有age 大于28的記錄,只返回age和sex | db.getCollection('example_data_1').aggregate([ | 聚合查詢 |
| 在“$project”的Value字典中添加一個不存在的字段 | db.getCollection('example_data_1').aggregate([ | 聚合查詢 |
| 上面代碼中的“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([ | 聚合查詢 |
| 使用find(),想返回“user_id”和“name”, | db.getCollection('example_data_2').find({}, {'user.name': 1, | 聚合查詢 |
| 使用“$project”,則可以把嵌套字段中的內容“抽取”出來,變成普通字段 | db.getCollection('example_data_2').aggregate([ | 聚合查詢 |
| 特殊字段的值normalstring和“$project”的自身語法沖突 | db.getCollection('example_data_1').aggregate([ | 聚合查詢 |
| 使用“distinct”函數去重 | db.getCollection('example_data_4').distinct('name') | 聚合查詢 |
| aggregate方式去重 | db.getCollection('example_data_4').aggregate([{'$group': {'_id': | 分組操作雖然也能實現去重操作,但是它返回的數據格式 與“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?_id:?0?to suppress the field.?
總結
以上是生活随笔為你收集整理的MongoDB的高级语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Method db.collection
- 下一篇: robo3t设置密码链接