mongodb查詢,mongodb

來源:互聯網
上載者:User

mongodb查詢,mongodb
1.查詢單條記錄

> db.choice.findOne()

2.查詢時指定查詢條件

> db.choice.find({"_id":"005a38d5"})

預設{}查詢全部

指定返回列

> db.choice.find({"_id":"005a38d5"},{"title":1,"results":1}){ "_id" : "005a38d5", "title" : "While some maintain that the recent proliferation of uncredited web sources will have a(n) (i)_____ effect on scholarship, others argue that the effects will be far more (ii)_____, claiming that academics are sensible enough not to (iii)_____ unattributed sources.", "results" : [  [  "1" ],  [  "1" ],  [  "0" ] ] }>

指定返回title和results屬性

預設會返回_id,也可以設定不返回

> db.choice.find({"_id":"005a38d5"},{"title":1,"results":1,"_id":0}){ "title" : "While some maintain that the recent proliferation of uncredited websources will have a(n) (i)_____ effect on scholarship, others argue that the effects will be far more (ii)_____, claiming that academics are sensible enough not to (iii)_____ unattributed sources.", "results" : [  [  "1" ],  [  "1" ],  ["0" ] ] }>

3.條件查詢


條件操作符
"$lt"===============>"<"
"$lte"==============>"<="
"$gt"===============>">"
"$gte"==============>">="
"$ne"==============>"!="


查詢出blankCount大於等於1,小於等於2的一條記錄
> db.choice.findOne({"blankCount":{$lte:2,$gte:1}},{"blankCount":1}){ "_id" : "006526ff", "blankCount" : 2 }>

4.$or或
> db.choice.findOne({$or:[{"blankCount":2},{"type":3}]})

查詢出blankCount為2或者type為3的一條記錄


5.$not

> db.choice.findOne({"type":{"$not":{$gt:3}}})


查詢出type不大於3的一條記錄

6.查詢空null的記錄

> db.questionSet.findOne({source:null})


設定欄位為null後,欄位為null或者不包含該欄位的記錄也會匹配。


如果不查詢不存在的欄位,則使用$exists:true


> db.questionSet.findOne({source:null,$exists:true})

7.查詢時使用Regex

> db.choice.findOne({title:/^While/})

查詢title以While開頭的一條記錄

8.數組查詢

> db.questionSet.findOne({"questionIds":'6188e9fc'},{"questionIds":1}){        "_id" : "030eeeba",        "questionIds" : [                "6188e9fc",                "a380e38c",                "addff709",                "b6bc4eff",                "5095b99f",                "c8352e48",                "ecca3626",                "c31125f7"        ]}



查詢數組questionIds中包含6188e9fc的記錄


如果查詢多個元素在數組中用$all,其中不分順序。
> db.questionSet.findOne({"questionIds":{$all:['6188e9fc','a380e38c']}},{"questionIds":1}){        "_id" : "030eeeba",        "questionIds" : [                "6188e9fc",                "a380e38c",                "addff709",                "b6bc4eff",                "5095b99f",                "c8352e48",                "ecca3626",                "c31125f7"        ]}

精確匹配:
> db.questionSet.findOne({"questionIds":['6188e9fc','a380e38c']},{"questionIds":1})null


9.$slice操作符
取出數組中的前3條記錄
> db.questionSet.findOne({},{"questionIds":{$slice:3}}){        "_id" : "030eeeba",        "catQuestionSet" : 2,        "orderNo" : 2,        "source" : 1,        "type" : 2,        "level" : 3,        "questionCount" : 10,        "questionIds" : [                "6188e9fc",                "a380e38c",                "addff709"        ]}

取出數組中的後3條記錄
> db.questionSet.findOne({},{"questionIds":{$slice:-3}}){        "_id" : "030eeeba",        "catQuestionSet" : 2,        "orderNo" : 2,        "source" : 1,        "type" : 2,        "level" : 3,        "questionCount" : 10,        "questionIds" : [                "c8352e48",                "ecca3626",                "c31125f7"        ]}

10.內嵌文檔查詢


查詢文檔有兩種方式,一種是完全匹查詢,另一種是針對鍵值對查詢。內嵌文檔的完全符合查詢和數組的完全符合查詢一樣,內嵌文檔內鍵值對的數量,順序都必須一致才會匹配。

完全符合:
> db.choice.findOne({"explain":{"ccurlList":"3DC334A16B187EBF9C33DC5901307461","textExplain":"Answers"}})

鍵值對匹配(常用):
> db.choice.findOne({"explain.ccurlList":"3DC334A16B187EBF9C33DC5901307461","explain.textExplain":"Answers"})

數組中單條文檔進行匹配時,使用$elemMatch
> db.choice.findOne({"explain":{$elemMatch:{"ccurlList":"3DC334A16B187EBF9C33DC5901307461","textExplain":"Answers"}}})

11.$where查詢
查詢出blankCount和type相等的一條記錄
> db.choice.findOne({$where:"this.blankCount==this.type"}){        "_id" : "005a38d5",        "blankCount" : 3,        "explain" : {                "ccurlList" : [ ]        },        "type" : 3,        "questionSetId" : "affccc14"     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.