1. 程式人生 > >MongoDB limit 選取 skip跳過 sort排序 方法

MongoDB limit 選取 skip跳過 sort排序 方法

skip mic sql bold nbsp () 羽毛 足球 mon

MongoDB limit 選取 skip跳過 sort排序

在mysql裏有order by MongoDB用sort代替order by

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 
3, "name" : "peter", "age" : 21 } { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "籃球", "足球" ] } { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 } { "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" :
6, "name" : "Mary", "age" : 84, "gender" : "男" }

Limit 選取 :

我要從這些 Document 中取出多少個

選取2條Document

> db.user.find().limit(2)
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84
, "gender" : "男" }

從整個collection集合的第一條數據Document開始選取兩條

Skip 跳過 :

我要跳過多少個Document

我要跳過前兩個 Document 直接從第三個Document 開始

> db.user.find().skip(2)
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "籃球", "足球" ] }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

skip(2) 就是跳過兩條Document,從整個collection集合第一條數據Document開始跳過

Limit + Skip :

我只想要第二條和第三條

> db.user.find()
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "籃球", "足球" ] }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
> 
> db.user.find().skip(1).limit(2)
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }

跳過第一條數據,從第二條數據開始選取2條數據Document

反過來順序是一樣的

> db.user.find().limit(2).skip(1)
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }

選取兩條數據,但要跳過第一條數據,從第二條數據開始選取

Sort 排序 : 將結果按照關鍵字排序

sort寫法 {} 裏面根據什麽Field進行排序

1 為升序 , -1 為降序

db.user.find().sort({ sortField:-1 })

將find出來的Document 按照 age 進行 升序 | 降序 排列

升序

> db.user.find().sort({"age":1})
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "籃球", "足球" ] }
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }

降序

> db.user.find().sort({"age":-1})
{ "_id" : ObjectId("5ca7a4b7219efd687462f966"), "id" : 2, "name" : "mike", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "籃球", "足球" ] }
{ "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f967"), "id" : 3, "name" : "peter", "age" : 21 }

Limit + Skip + Sort 混搭:

選取第二條第三條第四條 並 按照 age 進行 降序排列

> db.user.find().skip(1).limit(3).sort({age:-1})
{ "_id" : ObjectId("5ca7a505219efd687462f96a"), "id" : 6, "name" : "Mary", "age" : 84, "gender" : "男" }
{ "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
{ "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "羽毛球", "籃球", "足球" ] }

MongoDB limit 選取 skip跳過 sort排序 方法