Hướng dẫn mongodb find sort - mongodb tìm sắp xếp

This entry is part 14 of 24 in the series MongoDB

  • MongoDB là gì? Các khái niệm trong MongoDB
  • Hướng dẫn cài đặt, cấu hình MongoDB trên Windows 10
  • Cài đặt và sử dụng Robo 3T [RoboMongo] trên Windows
  • Hướng dẫn cài đặt MongoDB trên Linux [Ubuntu 16.04]
  • Hướng dẫn MongoDB – Tạo database trong MongoDB
  • Hướng dẫn MongoDB – Xóa, DROP database trong MongoDB
  • Tạo bảng, collections trong MongoDB [Tạo bằng dòng lệnh, Robo3t]
  • Xóa bảng, collections trong MongoDB [Xóa bằng lệnh/ Robo3T]
  • Insert document, bản ghi trong MongoDB [Insert bằng lệnh, Robo3T]
  • Truy vấn dữ liệu/document, find[], Select, Where trong MongoDB
  • Xóa document, row trong MongoDB [bằng dòng lệnh, Robo3T]
  • Update document, dữ liệu trong MongoDB
  • Projection trong MongoDB [SELECT field/column trong MongoDB]
  • Sắp xếp trong MongoDB[orderBy, sorting[] trong MongoDB]
  • Ưu nhược điểm của MongoDB, khi nào nên dùng MongoDB
  • Xóa cột, field, trường của collections trong MongoDB [$unset]
  • Kiểm tra null, check tồn tại trong MongoDB với $exists
  • Đổi tên field trong MongoDB với $rename
  • Tạo user/roles, phân quyền người dùng trên MongoDB
  • Các loại roles, vai trò, quyền trong MongoDB.
  • Đăng nhập mongodb với username và password [database mongo]
  • Tạo Replica Set trong MongoDB, Ví dụ Replica Set MongoDB
  • Replication, Replica Set trong MongoDB là gì?
  • Phân trang trong MongoDB [skip[], limit[] paging trong MongoDB]

Sắp xếp trong MongoDB[orderBy, sorting[] trong MongoDB]

Ưu nhược điểm của MongoDB, khi nào nên dùng MongoDB

Xóa cột, field, trường của collections trong MongoDB [$unset]

Kiểm tra null, check tồn tại trong MongoDB với $exists

>db.COLLECTION_NAME.find[].sort[{field1:1, field2:-1,...}]

Đổi tên field trong MongoDB với $rename

  • Tạo user/roles, phân quyền người dùng trên MongoDB
  • Các loại roles, vai trò, quyền trong MongoDB.

Đăng nhập mongodb với username và password [database mongo]

{'_id':'1', 'name':'neymar', 'country':'brazil', 'age':25},
{'_id':'2', 'name':'hazard', 'country':'belgium', 'age':25},
{'_id':'3', 'name':'mbappe', 'country':'france', 'age':18},
{'_id':'4', 'name':'modric', 'country':'croatia', 'age':30},
{'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':33},
{'_id':'6', 'name':'messi', 'country':'argentina', 'age':31},
{'_id':'7', 'name':'icardi', 'country':'argentina', 'age':25},
{'_id':'8', 'name':'griezmann', 'country':'france', 'age':28}

Tạo Replica Set trong MongoDB, Ví dụ Replica Set MongoDB

db.player.find[].sort[{'name':1}]

Replication, Replica Set trong MongoDB là gì?

Phân trang trong MongoDB [skip[], limit[] paging trong MongoDB]

db.player.find[].sort[{'country':1, 'age':-1}]

Mặc định kết quả truy vấn của method find[] trả về sẽ sắp xếp theo field _id

References:

//docs.mongodb.com/manual/reference/operator/meta/orderby/

Để tùy chỉnh cách sắp xếp kết quả truy vấn ta dùng method sort[] ở sau method find[]

Cú pháp:

Trong đó:

  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':25},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':25},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':18},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':30},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':33},
    {'_id':'6', 'name':'messi', 'country':'argentina', 'age':31},
    {'_id':'7', 'name':'icardi', 'country':'argentina', 'age':25},
    {'_id':'8', 'name':'griezmann', 'country':'france', 'age':28}
    
    1 tức là sắp xếp tăng dần theo field1
  • {'_id':'1', 'name':'neymar', 'country':'brazil', 'age':25},
    {'_id':'2', 'name':'hazard', 'country':'belgium', 'age':25},
    {'_id':'3', 'name':'mbappe', 'country':'france', 'age':18},
    {'_id':'4', 'name':'modric', 'country':'croatia', 'age':30},
    {'_id':'5', 'name':'ronaldo', 'country':'portugal', 'age':33},
    {'_id':'6', 'name':'messi', 'country':'argentina', 'age':31},
    {'_id':'7', 'name':'icardi', 'country':'argentina', 'age':25},
    {'_id':'8', 'name':'griezmann', 'country':'france', 'age':28}
    
    2 tức là sắp xếp giảm dần theo field2

Kiểm tra null, check tồn tại trong MongoDB với $exists

db.COLLECTION_NAME.find []. sort [{KEY: 1}]

Đổi tên field trong MongoDB với $rename

Tạo user/roles, phân quyền người dùng trên MongoDB

[  
  {  
    Course: "Java",  
    details: { Duration: "6 months", Trainer: "Sonoo Jaiswal" },  
    Batch: [ { size: "Medium", qty: 25 } ],  
    category: "Programming Language"  
  },  
  {  
    Course: ".Net",  
    details: { Duration: "6 months", Trainer: "Prashant Verma" },  
    Batch: [ { size: "Small", qty: 5 }, { size: "Medium", qty: 10 }, ],  
    category: "Programming Language"  
  },  
  {  
    Course: "Web Designing",  
    details: { Duration: "3 months", Trainer: "Rashmi Desai" },  
    Batch: [ { size: "Small", qty: 5 }, { size: "Large", qty: 10 } ],  
    category: "Programming Language"  
  }  
];  

Các loại roles, vai trò, quyền trong MongoDB.

db.test.find []. sort [{"Course": - 1}]

Đăng nhập mongodb với username và password [database mongo]

Bài Viết Liên Quan

Chủ Đề