mongoose?


설치

npm install mongoose

mongoose connection

mongo.js

var mongoose = require('mongoose');
var dbconfig = require(__dirname+'/../config/db-config.json');
mongoose.connect('mongodb://'+dbconfig["mongoUser"]+':' + dbconfig["mongoPw"] + '@127.0.0.1/{DBname}?authSource=admin');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('mono open')
});

exports._ = mongoose



model

eventLogModel.js

var eventLog = new Schema(
	{
		_id:String,
		label:String

	},{ collection: '{collectionName}'}
);

exports.eventLog = mongo._.model({모델이름},eventLog);


save

@@LogController.js

const eventLogModel = require(__dirname+'/../model/eventLogModel.js');

var eventLog = new eventLogModel({
label:"안녕하세요 이벤트로그 샘플이에요"
})
eventLog.save().then(function (doc){
	console.log('saved data => ',doc)
}))


find

 eventLogModel.find({},function(err,data){
 	if (err) return handleError(err);
 	console.log('find all data => ',data)
 })



마치며