• Q&A
  • 회원가입
  • 로그인

[mongodb] Node.js - MongoDB Sort (데이터 정렬)

오름차순 정렬

 

sort() 메서드를 사용하여 결과를 오름차순 또는 내림차순으로 정렬.

sort()메서드는 정렬 순서를 정의하는 객체인 1개의 매개변수 사용.

 


1. C:\User\사용자명\hz.js 생성.

 

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:27017/";

const options = {useUnifiedTopology: true}; 


MongoClient.connect(url, options, function(err, db) {

  if (err) throw err;

  var dbo = db.db("hz");

  var mysort = { mb_name: 1 };

  dbo.collection("hz_member").find().sort(mysort).toArray(function(err, result) {

    if (err) throw err;

    console.log(result);

    db.close();

  });

});

 


2. CMD 모드 창에서, 아래 명령어 실행.

 

C:\User\사용자명>node hz.js

 


[결과값] 

 

[

  { _id: 5f69b3b2a650682498a08889, mb_name: 'AAA', mb_level: '1' },

  { _id: 5f69b3b2a650682498a0888a, mb_name: 'BBB', mb_level: '2' },

  { _id: 5f69b3b2a650682498a0888b, mb_name: 'CCC', mb_level: '2' },

  { _id: 5f69b3b2a650682498a0888c, mb_name: 'DDD', mb_level: '3' },

  { _id: 5f69b3b2a650682498a0888d, mb_name: 'EEE', mb_level: '3' },

  { _id: 5f69b3b2a650682498a0888e, mb_name: 'FFF', mb_level: '3' },

  { _id: 5f69b3b2a650682498a0888f, mb_name: 'GGG', mb_level: '4' },

  { _id: 5f69b3b2a650682498a08890, mb_name: 'HHH', mb_level: '4' },

  { _id: 5f69b3b2a650682498a08891, mb_name: 'III', mb_level: '4' },

  { _id: 5f69b3b2a650682498a08892, mb_name: 'JJJ', mb_level: '4' },

  { _id: 5f69b3b2a650682498a08893, mb_name: 'KKK', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08894, mb_name: 'LLL', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08895, mb_name: 'MMM', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08896, mb_name: 'NNN', mb_level: '5' },

  { _id: 101, mb_name: 'OOO' },

  { _id: 102, mb_name: 'PPP' },

  { _id: 103, mb_name: 'QQQ' },

  {

    _id: 5f69b3995edfd806d4abfd60,

    mb_name: 'homzzang',

    mb_level: '10'

  }

]

 

 

내림차순 정렬

 

※ sort() 메서드의 매개변수 의미.

{mb_name: 1} : 오름차순 정렬.
{mb_name: -1} : 내림차순 정렬.

 


1. C:\User\사용자명\hz.js 생성.

 

var MongoClient = require('mongodb').MongoClient;

var url = "mongodb://localhost:27017/";

const options = {useUnifiedTopology: true}; 


MongoClient.connect(url, options, function(err, db) {

  if (err) throw err;

  var dbo = db.db("hz");

  var mysort = { mb_name: -1 };

  dbo.collection("hz_member").find().sort(mysort).toArray(function(err, result) {

    if (err) throw err;

    console.log(result);

    db.close();

  });

});

 


2. CMD 모드 창에서, 아래 명령어 실행.

 

C:\User\사용자명>node hz.js

 


[결과값] 

 

[

  {

    _id: 5f69b3995edfd806d4abfd60,

    mb_name: 'homzzang',

    mb_level: '10'

  },

  { _id: 103, mb_name: 'QQQ' },

  { _id: 102, mb_name: 'PPP' },

  { _id: 101, mb_name: 'OOO' },

  { _id: 5f69b3b2a650682498a08896, mb_name: 'NNN', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08895, mb_name: 'MMM', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08894, mb_name: 'LLL', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08893, mb_name: 'KKK', mb_level: '5' },

  { _id: 5f69b3b2a650682498a08892, mb_name: 'JJJ', mb_level: '4' },

  { _id: 5f69b3b2a650682498a08891, mb_name: 'III', mb_level: '4' },

  { _id: 5f69b3b2a650682498a08890, mb_name: 'HHH', mb_level: '4' },

  { _id: 5f69b3b2a650682498a0888f, mb_name: 'GGG', mb_level: '4' },

  { _id: 5f69b3b2a650682498a0888e, mb_name: 'FFF', mb_level: '3' },

  { _id: 5f69b3b2a650682498a0888d, mb_name: 'EEE', mb_level: '3' },

  { _id: 5f69b3b2a650682498a0888c, mb_name: 'DDD', mb_level: '3' },

  { _id: 5f69b3b2a650682498a0888b, mb_name: 'CCC', mb_level: '2' },

  { _id: 5f69b3b2a650682498a0888a, mb_name: 'BBB', mb_level: '2' },

  { _id: 5f69b3b2a650682498a08889, mb_name: 'AAA', mb_level: '1' }

]

 


홈짱닷컴 Homzzang.com

방문자 수

오늘 717
어제 1,479
최대 4,391
이달 36,443
전체 1,222,926
찾아주셔서 감사합니다. Since 2012