MongoDB 查詢整理

來源:互聯網
上載者:User

標籤:bsp   指定   分享圖片   查詢   分享   通過   com   span   取資料   

  • 查詢所有
    sql:  select * from table_name
    mongodb:   db.getCollection(‘期刊論文‘).find({})

    如所示,擷取期刊論文collection 下的所有 資料

  • 條件查詢
    注意: 請注意選擇列的資料類型
    1. journal _title = ‘ Nature‘
    sql: select * from table_name where journal_title = ‘Nature‘
    mongodb: db.getCollection(‘期刊論文‘).find({"journal_title": "Nature"})

    2. volume<495:
    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}}}])


  • 排序
    在MongoDB中使用使用sort()方法對資料進行排序,sort()方法可以通過參數指定排序的欄位,並使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而-1是用於降序排列

 

MongoDB 查詢整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.