查詢條件github
首先往數據庫集合裏面插入幾條數據。
測試數據:web
> db.users.insert({username:"mongo", url:"webinglin.github.io", tags:["mongodb", database","nosql"],likes:999, author:"linwenbin"}) > db.users.insert({username:"redis", url:"webinglin.github.io", tags:["redis","database","nosql"],likes:888, author:"linwenbin"}) > db.users.insert({username:"spring", url:"webinglin.github.io", tags:["spring","framework"],likes:777, author:"linwenbin"}) > db.users.find().pretty() { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } { "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" } { "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
pretty() 方法是對查詢結果進行格式化
查詢的時候能夠帶上查詢條件,那具體的查詢條件怎麼使用?
等於
等於操做直接使用 {key:value} 這樣的文檔形式便可redis
> db.users.find({username:"mongo"}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } >
大於
語法: {key : {$gt:value} }spring
> db.users.find({likes:{$gt:888}}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } >
大於等於
語法: {key : {$gte:value} }sql
> db.users.find({likes:{$gte:888}}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } { "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
小於
語法: {key : {$lt:value} }mongodb
> db.users.find({likes:{$lt:888}}) { "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
小於等於數據庫
語法: {key : {$lte:value}}數組
> db.users.find({likes:{$lte:888}}) { "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" } { "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
不等於
語法: {key : {$ne:value} }nosql
> db.users.find({likes:{$ne:888}}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } { "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" }
且操做 AND
語法: {key1:value1, key2:value2, key3:value3 …}
> db.users.find({likes:{$gt:777},username:"mongo"}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } > db.users.find({likes:{$gt:777}}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } { "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
或操做 OR
語法: { $or: [ {key1: value1}, {key2:value2} ] } 將or條件的全部 {key:value} 都放在 $or 的value中(數組)
> db.users.find({$or:[{username:"mongo"},{username:"redis"}]}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } { "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" }
複雜條件查詢
如何將全部的條件都連起來用呢?
好比咱們想要這樣查詢 like>=888 && (username=」mongo」 or username=」spring」)
因爲上面的數據只有三條, 咱們知道 like>=888 只有 mongo 和 redis 這兩條數據知足條件, 後面的username=」mongo」 or username=」spring」 又有 mongo和 spring 知足條件, 這兩個and操做以後 就只剩下 mongo 這條數據知足條件了。 因此最終應該查出一條mongo的Document.
> db.users.find({likes:{$gte:888},$or:[{username:"mongo"},{username:"spring"}]}) { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } >
find() 其餘用法
Projection
mongodb中 projection 意味着顯示你但願看到的字段而非全部的字段都顯示,這是什麼意思呢?
好比: 咱們的測試數據裏面有那麼多的字段: username,likes,tags,author,url 而咱們常常要用到的就只有 username 和 likes 那麼就顯示這兩個字段就行了,其餘的字段就別顯示出來了。
find({},{KEY:1/0}) find的第二個參數,KEY爲要顯示或隱藏的字段,value爲1表示顯示,0表示隱藏,看着也很簡單,試一下吧
> db.users.find({},{_id:0,url:0,tags:0,author:0}) { "username" : "mongo", "likes" : 999 } { "username" : "redis", "likes" : 888 } { "username" : "spring", "likes" : 777 } >
limit, skip, sort
爲了更好的測試分頁的效果,新建一個集合,並插入30條數據
> for(var i=0; i<30; i++){ ... db.pages.insert({"val":i}); ... } WriteResult({ "nInserted" : 1 }) > db.pages.find() { "_id" : ObjectId("5574ca7b192e9dda0925e37f"), "val" : 0 } { "_id" : ObjectId("5574ca7b192e9dda0925e380"), "val" : 1 } { "_id" : ObjectId("5574ca7b192e9dda0925e381"), "val" : 2 } { "_id" : ObjectId("5574ca7b192e9dda0925e382"), "val" : 3 } { "_id" : ObjectId("5574ca7b192e9dda0925e383"), "val" : 4 } { "_id" : ObjectId("5574ca7b192e9dda0925e384"), "val" : 5 } { "_id" : ObjectId("5574ca7b192e9dda0925e385"), "val" : 6 } { "_id" : ObjectId("5574ca7b192e9dda0925e386"), "val" : 7 } { "_id" : ObjectId("5574ca7b192e9dda0925e387"), "val" : 8 } { "_id" : ObjectId("5574ca7b192e9dda0925e388"), "val" : 9 } { "_id" : ObjectId("5574ca7b192e9dda0925e389"), "val" : 10 } { "_id" : ObjectId("5574ca7b192e9dda0925e38a"), "val" : 11 } { "_id" : ObjectId("5574ca7b192e9dda0925e38b"), "val" : 12 } { "_id" : ObjectId("5574ca7b192e9dda0925e38c"), "val" : 13 } { "_id" : ObjectId("5574ca7b192e9dda0925e38d"), "val" : 14 } { "_id" : ObjectId("5574ca7b192e9dda0925e38e"), "val" : 15 } { "_id" : ObjectId("5574ca7b192e9dda0925e38f"), "val" : 16 } { "_id" : ObjectId("5574ca7b192e9dda0925e390"), "val" : 17 } { "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 } { "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 } Type "it" for more
> db.pages.find().limit(5) { "_id" : ObjectId("5574ca7b192e9dda0925e37f"), "val" : 0 } { "_id" : ObjectId("5574ca7b192e9dda0925e380"), "val" : 1 } { "_id" : ObjectId("5574ca7b192e9dda0925e381"), "val" : 2 } { "_id" : ObjectId("5574ca7b192e9dda0925e382"), "val" : 3 } { "_id" : ObjectId("5574ca7b192e9dda0925e383"), "val" : 4 }
能夠發現,若是使用 limit方法的話會顯示整個集合的全部文檔。 指定了 limit 以後, 顯示具體的條數,上文中,limit(5) 表示, 顯示5條文檔。
limit方法除外,還有一個 skip 方法,skip也是接受一個整型的參數,表示查詢結果跳過多少個文檔。
例如上面插入的30條記錄中,咱們要顯示18-22條記錄,那麼就應該使用db.pages.find().skip(18).limit(5)
> db.pages.find().skip(18).limit(5) { "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 } { "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 } { "_id" : ObjectId("5574ca7b192e9dda0925e393"), "val" : 20 } { "_id" : ObjectId("5574ca7b192e9dda0925e394"), "val" : 21 } { "_id" : ObjectId("5574ca7b192e9dda0925e395"), "val" : 22 }
skip和limit的組合就能作到分頁的功能了。可是若是數據量很大,理論上分頁就會變得很慢了,好比有一億條數據,要拿最後一頁。那skip的數據量就不少不少了。這樣就會變得比較慢。話說回來,有誰會看數據看到最後的幾頁?正常都是看前面幾頁數據,因此,skip和limit實現分頁是能夠接受的。
在mongodb中,若是要對查詢結果排序,那麼須要使用sort方法。sort方法接收一個文檔參數。也就是{key:value}的形式。其中,key表示要排序的字段,value的可取值爲 1 / -1 。1表示升序asc,-1表示降序desc。話很少說,直接上例子:
> db.pages.find().sort({val:-1}) { "_id" : ObjectId("5574ca7b192e9dda0925e39c"), "val" : 29 } { "_id" : ObjectId("5574ca7b192e9dda0925e39b"), "val" : 28 } { "_id" : ObjectId("5574ca7b192e9dda0925e39a"), "val" : 27 } { "_id" : ObjectId("5574ca7b192e9dda0925e399"), "val" : 26 } { "_id" : ObjectId("5574ca7b192e9dda0925e398"), "val" : 25 } { "_id" : ObjectId("5574ca7b192e9dda0925e397"), "val" : 24 } { "_id" : ObjectId("5574ca7b192e9dda0925e396"), "val" : 23 } { "_id" : ObjectId("5574ca7b192e9dda0925e395"), "val" : 22 } { "_id" : ObjectId("5574ca7b192e9dda0925e394"), "val" : 21 } { "_id" : ObjectId("5574ca7b192e9dda0925e393"), "val" : 20 } { "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 } { "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 } { "_id" : ObjectId("5574ca7b192e9dda0925e390"), "val" : 17 } { "_id" : ObjectId("5574ca7b192e9dda0925e38f"), "val" : 16 } { "_id" : ObjectId("5574ca7b192e9dda0925e38e"), "val" : 15 } { "_id" : ObjectId("5574ca7b192e9dda0925e38d"), "val" : 14 } { "_id" : ObjectId("5574ca7b192e9dda0925e38c"), "val" : 13 } { "_id" : ObjectId("5574ca7b192e9dda0925e38b"), "val" : 12 } { "_id" : ObjectId("5574ca7b192e9dda0925e38a"), "val" : 11 } { "_id" : ObjectId("5574ca7b192e9dda0925e389"), "val" : 10 } Type "it" for more
這個是對val這個key進行逆序排序,因此value取值爲-1。 那value值爲1的話,就變成升序了。
> db.pages.find().sort({val:1}) { "_id" : ObjectId("5574ca7b192e9dda0925e37f"), "val" : 0 } { "_id" : ObjectId("5574ca7b192e9dda0925e380"), "val" : 1 } { "_id" : ObjectId("5574ca7b192e9dda0925e381"), "val" : 2 } { "_id" : ObjectId("5574ca7b192e9dda0925e382"), "val" : 3 } { "_id" : ObjectId("5574ca7b192e9dda0925e383"), "val" : 4 } { "_id" : ObjectId("5574ca7b192e9dda0925e384"), "val" : 5 } { "_id" : ObjectId("5574ca7b192e9dda0925e385"), "val" : 6 } { "_id" : ObjectId("5574ca7b192e9dda0925e386"), "val" : 7 } { "_id" : ObjectId("5574ca7b192e9dda0925e387"), "val" : 8 } { "_id" : ObjectId("5574ca7b192e9dda0925e388"), "val" : 9 } { "_id" : ObjectId("5574ca7b192e9dda0925e389"), "val" : 10 } { "_id" : ObjectId("5574ca7b192e9dda0925e38a"), "val" : 11 } { "_id" : ObjectId("5574ca7b192e9dda0925e38b"), "val" : 12 } { "_id" : ObjectId("5574ca7b192e9dda0925e38c"), "val" : 13 } { "_id" : ObjectId("5574ca7b192e9dda0925e38d"), "val" : 14 } { "_id" : ObjectId("5574ca7b192e9dda0925e38e"), "val" : 15 } { "_id" : ObjectId("5574ca7b192e9dda0925e38f"), "val" : 16 } { "_id" : ObjectId("5574ca7b192e9dda0925e390"), "val" : 17 } { "_id" : ObjectId("5574ca7b192e9dda0925e391"), "val" : 18 } { "_id" : ObjectId("5574ca7b192e9dda0925e392"), "val" : 19 } Type "it" for more >
那若是要對多個值進行組合排序呢? 就比如如對於咱們最初的例子 users 集合。 要對users集合進行排序。其中 按照likes升序, 按照username降序。爲了達到咱們的效果,咱們再往users集合裏面插入兩條數據
> db.users.insert({username:"mongodb",likes:999}) > db.users.insert({username:"springMVC",likes:888})
下面是運行結果,注意觀察 likes爲888的兩個文檔。發現username逆序排序了。至此,說明咱們的sort實驗成功了。
> db.users.find().sort({likes:1,username:-1}).pretty() { "_id" : ObjectId("5574bdf3c705777157a515ac"), "username" : "spring", "url" : "webinglin.github.io", "tags" : [ "spring", "framework" ], "likes" : 777, "author" : "linwenbin" } { "_id" : ObjectId("5574cefa192e9dda0925e39e"), "username" : "springMVC", "likes" : 888 } { "_id" : ObjectId("5574bdd2c705777157a515ab"), "username" : "redis", "url" : "webinglin.github.io", "tags" : [ "redis", "database", "nosql" ], "likes" : 888, "author" : "linwenbin" } { "_id" : ObjectId("5574cef5192e9dda0925e39d"), "username" : "mongodb", "likes" : 999 } { "_id" : ObjectId("5574bdabc705777157a515aa"), "username" : "mongo", "url" : "webinglin.github.io", "tags" : [ "mongodb", "database", "nosql" ], "likes" : 999, "author" : "linwenbin" } >
原做者:webinglin