日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MongoDB的高级语法

發布時間:2023/12/31 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?_id:?0?to suppress the field.?

總結

以上是生活随笔為你收集整理的MongoDB的高级语法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。