본문 바로가기

프로그래밍/Node.js

Mongoose 사용하기.

1. Mongoose 란?

 - mongoose는 ODM Framework 이다.

 - rdb에서 편하게 사용하기 위해 만들어진 'ORM'과 같은 개념을 nosql에서는 'ODM'이라고 부른다.


2. Mongoose Install

npm install mongoose



3. Sample Source

var mongoose = require('mongoose');

var schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/mongoose');


var TestSchma = new schema({

    title : String,  

    hit_cnt : Number, 

    date : Date    

});


var TestModel = mongoose.model('test', TestSchma);


var testM = new TestModel();


testM.title = "Mongoose Test Data";

testM.hit_cnt = 0;

testM.date = new Date();


testM.save(function (err) {

    if (err) throw err; 

    

console.log('true');

});


TestModel.find({}, function(err, data){

if (err) throw err;

console.log(data);

});



'프로그래밍 > Node.js' 카테고리의 다른 글

npm에서 --save, --save-dev 옵션  (0) 2018.05.02