[NodeJs] NoSQL MongoDB 연동하기 (검색/추가/삭제/갱신)
설치하기
https://www.npmjs.com/package/mongojs
1. npm install mongodbjs
- 구현
- Connection
var mongojs = require('mongojs');
var db = mongojs('localhost/test', ['board']);
db.on('error', function(err) {
console.log(err);
});
db.on('connection', function() {
console.log('connected');
});
- Find
db.board.find(function(err, docs) {
console.log(arguments);
for (var i = 0; i <docs.length; i++) {
console.log(docs[i].id + ", " + docs[i].title +", " +
docs[i].content +", " + docs[i].writer);
}
});
db.collection.find(function(err,array){});
db.collection.find({cKey:cVal}).toArray(function(err,array){});
db.collection.find({cKey:cVal},{sKey:true, sKey:false}).toArray(function(err,array){});
db.collection.findOne({cKey:cVal}, function(err,obj){});
db.collection.find().sort({name:1}, function(err, array){});
- Save / Insert
db.board.insert({_id:5, title:'new', content:'new content', writer:'ggiggi'});
- Delete
db.board.remove({_id:3});
- Update
db.board.update({_id:2}, {$set:{writer:'hiru'}});
'JavaScript BackEnd > Node.js, Express' 카테고리의 다른 글
[NodeJs] 내장 모듈 사용하기 (os, url, querystring, util) (0) | 2021.04.30 |
---|---|
[NodeJs] NodeJs 시작하기 (이클립스(Eclipse) 설치 및 개발환경 설정) (2/2) (0) | 2021.04.30 |
[NodeJs] NodeJs 시작하기 (특징/설치) (1/2) (0) | 2021.04.30 |
[NodeJs] C++ Addon 하는 방법 (설치/속성/메서드/파라미터,리턴/callback/객체생성반환/함수생성/C++객체생성) (0) | 2021.04.30 |
[NodeJs] MySQL 연동하기 (SELECT/INSERT/UPDATE/DELETE) (0) | 2021.04.30 |
[NodeJs] Socket.io를 이용해 간단한 채팅(Chatting) 만들기. (0) | 2021.04.30 |
[NodeJs] Socket.io를 WebSocket 구현하기. (0) | 2021.04.30 |
[NodeJs] Socket.IO란? (0) | 2021.04.30 |