- 查詢所有
sql: select * from table_name
mongodb: db.getCollection('期刊論文').find({})如上圖所示,獲取期刊論文collection 下的所有 數據
-
條件查詢
2. volume<495:
注意: 請注意選擇列的數據類型
1. journal _title = ' Nature'
sql: select * from table_name where journal_title = 'Nature'
mongodb: db.getCollection('期刊論文').find({"journal_title": "Nature"})
sql: select * from table_name where volume< 495;
mongodb: db.getCollection('期刊論文').find({"volume":{$lt:"495"}}).pretty()3. volume <= 495
sql: select * from table_name where volume<= 495;
mongodb: db.getCollection('期刊論文').find({"volume":{$lte:"495"}}).pretty()
4. volume >495
sql: select * from table_name where volume> 495;
mongodb: db.getCollection('期刊論文').find({"volume":{$gt:"495"}}).pretty()
5. volume !=495
sql: select * from table_name where volume!= 495;
mongodb: db.getCollection('期刊論文').find({"volume":{$ne:"495"}}).pretty()and, or 查詢
6. and 查詢:
sql: select * from table_name where journal_title = 'Nature' and vulome> 495;
mongodb: db.getCollection('期刊論文').find({"journal_title":"Nature","volume":{$gt:"495"}}).pretty()
7. or 查詢:
sql: select * from table_name where journal_title = 'Nature' or vulome> 495;
mongodb: db.getCollection('期刊論文').find({$or: [ {"journal_title":"Nature"},{"volume":{$gt:"495"}}]}).pretty()
8. and 和 or 聯合查詢:
sql: select * from table_name where volume>52 and journal_title = 'Nature' or journal_title = 'Meccanica';
mongodb: db.getCollection('期刊論文').find({"volume":{$gt:'52'},$or: [ {"journal_title":"Nature"},{"journal_title":"Meccanica"}]}).pretty()
-
獲取數據數量
sql: select count(*) from table_name
mongodb: db.getCollection('期刊論文').count() - 分組查詢
sql: select journal_title, count(*) from table_name group by journal_title
mongodb: db.getCollection('期刊論文').aggregate([{$group :{_id:"$journal_title", num_tutorial : {$sum:1}}}]) - 查詢指定列
sql: sellect title from table
Mongo:db.getCollection('coll').find({},{"title":1})db.getCollection('新聞資訊').aggregate([{$match:{'language':'英文',"title":{$regex:"[\u4e00-\u9fa5]"}}},{$group :{_id:"$domain", num_tutorial : {$sum:1}}}])
- 查詢存在指定列的數據
db.getCollection('新聞資訊').find({"date":{$exists: true }})
pymongo : collection.find({"zh_name":{ "$exists": True }}) - 導出數據到csv
mongoexport -h data ip -d dataname -c collection_name --csv -f field1,field2,…… -o d:\mongodb\energy.csv - 排序
在MongoDB中使用使用sort()方法對數據進行排序,sort()方法可以通過參數指定排序的字段,並使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而-1是用於降序排列