Node.js 조작 MongoDB 데이터베이스 실례 분석

5512 단어
본문의 실례는 Node를 서술하였다.js는 MongoDB 데이터베이스를 조작합니다.다음과 같이 여러분에게 참고할 수 있도록 공유합니다.
Node.js 운영 MongoDB

npm init
npm i mongodb --save


{
 "name": "test",
 "version": "1.0.0",
 "description": "",
 "main": "app.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "",
 "license": "ISC",
 "dependencies": {
  "mongodb": "^3.1.1"
 }
}


데이터베이스 연결

// connect.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 client.close();
});


삽입

// insert.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
//  
var insertData = function (db, callback) {
 //  
 var collection = db.collection('collection3');
 var data = [{"name": " 001", "age": 20}, {"name": " 002", "age": 21}];
 //  
 collection.insert(data, function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 insertData(db, function (result) {
  console.log(result);
  client.close();
 });
});


조회

// find.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
//  
var findData = function (db, callback) {
 //  
 var collection = db.collection('collection3');
 var whereStr = {"name": " 001"};
 //  
 collection.find(whereStr).toArray(function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 findData(db, function (result) {
  console.log(result);
  client.close();
 })
});


수정하다

// update.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
//  
var updateData = function (db, callback) {
 //  
 var collection = db.collection('collection3');
 var whereStr = {"name": " 002"};
 var updateStr = {$set: {"age": 100}};
 //  
 collection.update(whereStr, updateStr, function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 updateData(db, function (result) {
  console.log(result);
  client.close();
 })
});


삭제

// delete.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
//  
var delData = function (db, callback) {
 //  
 var collection = db.collection('collection3');
 var whereStr = {"name": " 002"};
 //  
 collection.remove(whereStr, function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
  delData(db, function (result) {
  console.log(result);
  client.close();
 })
});


참조:
https://www.npmjs.com/package/mongodb https://www.jb51.net/article/58815.htm https://www.jb51.net/article/98813.htm
본문에서 기술한 것이 모두에게 node가 되기를 바랍니다.js 프로그램 설계가 도움이 되었습니다.

좋은 웹페이지 즐겨찾기