스토리지

[05.12] 웹서버에서 MongoDB에 저장된 데이터를 읽어오기 본문

카테고리 없음

[05.12] 웹서버에서 MongoDB에 저장된 데이터를 읽어오기

ljw4104 2021. 5. 21. 16:21

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'Dialog';
const client = new MongoClient(url);
// Use connect method to connect to the server
client.connect(function(err) {
  assert.equal(null, err);
  console.log('Connected successfully to server');

  const db = client.db(dbName);
  findDocuments(db, function(){
    client.close();
  });
});

const findDocuments = function(db, callback) {
  // Get the documents collection
  const collection = db.collection('Prologue');
  // Find some documents
  collection.find({}).toArray(function(err, docs) {
    assert.equal(err, null);
    console.log('Found the following records');
    console.log(docs);
    callback(docs);
  });
};

그냥 공식문서에 있는 내용 그대로 베껴와서 해봤더니 너무 잘된다.

https://www.npmjs.com/package/mongodb

 

mongodb

The official MongoDB driver for Node.js

www.npmjs.com

 

Comments