Node.js: Sắp xếp dữ liệu trong bộ sưu tập (Sort)


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

Cập nhật MongoDB

Với bản cập nhật mới nhất của MongoDB, bạn cần cài đặt thêm package mongodb-legacy để sử dụng

npm install mongodb-legacy

Sắp xếp kết quả

Sử dụng phương thức sort() để sắp xếp kết quả theo thứ tự tăng dần hoặc giảm dần.

Phương thức sort() nhận một tham số, một đối tượng xác định thứ tự sắp xếp.

const { MongoClient } = require('mongodb-legacy');
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;
  var dbo = client.db("mydb");
 
var mysort = { name: 1 };
  dbo.collection("customers").find().sort(mysort).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    client.close();
  });
});

Với mongoose

const mongoose = require('mongoose');
const url = "mongodb://localhost:27017/mydb";
const schema = new mongoose.Schema({name: "string", address: "string"})
const userinfo = mongoose.model("userinfo", schema, "customers")

mongoose.connect(url, function(err) {
  if (err) throw err;
  userinfo.
find({}, null, {sort: {name: 1}}, function(err, result){
    if (err) throw err;
    console.
log(result)
    mongoose.connection.
close()
  })
});

Kết quả

 [
  {
    _id: new ObjectId("63459511e9a4188e722ee6f3"),
    name: 'Amy',
    address: 'Apple st 652'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f4"),
    name: 'Hannah',
    address: 'Mountain 21'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f1"),
    name: 'John',
    address: 'Highway 71'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f2"),
    name: 'Peter',
    address: 'Lowstreet 4'
  },
  {
    _id: new ObjectId("634595ca4b1816a4172ca50b"),
    name: 'V1Study',
    address: 'v1study.com'
  }
]

Sắp xếp theo thứ tự giảm dần

const { MongoClient } = require('mongodb-legacy');
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;
  var dbo = client.db("mydb");
  
var mysort = { name: -1 };
  dbo.collection("customers").find().sort(mysort).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    client.close();
  });
});

Với mongoose

const mongoose = require('mongoose');
const url = "mongodb://localhost:27017/mydb";
const schema = new mongoose.Schema({name: "string", address: "string"})
const userinfo = mongoose.model("userinfo", schema, "customers")

mongoose.connect(url, function(err) {
  if (err) throw err;
  userinfo.
find({}, null, {sort: {name: -1}}, function(err, result){
    if (err) throw err;
    console.
log(result)
    mongoose.connection.
close()
  })
});

Kết quả

 [
  {
    _id: new ObjectId("634595ca4b1816a4172ca50b"),
    name: 'V1Study',
    address: 'v1study.com'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f2"),
    name: 'Peter',
    address: 'Lowstreet 4'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f1"),
    name: 'John',
    address: 'Highway 71'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f4"),
    name: 'Hannah',
    address: 'Mountain 21'
  },
  {
    _id: new ObjectId("63459511e9a4188e722ee6f3"),
    name: 'Amy',
    address: 'Apple st 652'
  }
]

» Tiếp: Bắt đầu
« Trước: DROP TABLE và IF EXISTS
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!