Nodejs MongoDB 방문

5310 단어 mongodbnode.js
Setting. js [데이터베이스 정보 저장 에 사용]
module.exports = {
	name : 'ZMessage',
	host : 'localhost'
}

 name 은 데이터베이스 이름 이 고 host 는 데이터베이스 접근 주소 입 니 다.
 
DBHelper. js [데이터베이스 에 구체 적 으로 접근 하 는 방법]
function DBHelper(){
	this.dbSetting = require('./DBSettings.js');
	this.Db = require('mongodb').Db;
	this.Connection = require('mongodb').Connection;
	this.Server = require('mongodb').Server;
	this.GetDBExecutor = function(){
		return new this.Db(this.dbSetting.name, new this.Server(this.dbSetting.host, this.Connection.DEFAULT_PORT, {}),{safe:false});
	};
	//        
	this.getAllUser = function(callback){
		var executor = this.GetDBExecutor();
		executor.open(function(err, db) {
			if (err) {
				return callback(err);
			}
			db.collection('UserInfo', function(err, collection) {
				if (err) {
					executor.close();
					return callback(err);
				}
				collection.find({}).toArray(function(err,items){
					executor.close();
					items.forEach(function(item, index) {
						console.log(item.userName);
					});
				});
			});
		});
	}
	
	//      
	this.addUser = function(user,callback){
		var executor = this.GetDBExecutor();
		executor.open(function(err, db) {
			if (err) {
				return callback(err);
			}
			db.collection('UserInfo', function(err, collection) {
				console.log(4);
				if (err) {
					executor.close();
					return callback(err);
				}
				collection.insert(user, {safe: true}, function(err, user) {
					executor.close();	
				});
			});
		});
	}
	
	//      
	this.updateUser = function(user, callback){
		var executor = this.GetDBExecutor();
		executor.open(function(err, db){
			if (err) {
				return callback(err);
			}
			db.collection('UserInfo', function(err, collection){
				if (err) {
					executor.close();
					return callback(err);
				}
				collection.update({
					"userId": user.userId
				}, {
					$set: {
						userName: "   "
					}
				}, {
					saft: false,
					upsert: true
				}, function(err, user){
					executor.close();
				});
			});
		});
	}
	//      
	this.deleteUser = function(userId,callback){
		var executor = this.GetDBExecutor();
		executor.open(function(err, db) {
			if (err) {
				return callback(err);
			}
			db.collection('UserInfo', function(err, collection) {
				if (err) {
					executor.close();
					return callback(err);
				}
				collection.remove({"userId":userId},{saft:false},function(err, user) {
					executor.close();	
				});
			});
		});
	}
}
module.exports = new DBHelper();

 User Info 집합 이 이미 존재 한다 고 가정 합 니 다.
 
app. js 시작 파일 테스트 에 사용
var dbclient = require('./DBHelper/DBHelper');

dbclient.addUser({userId:3,userName:'  ',userStatus : false},function(err){
	console.log(err);
});

dbclient.getAllUser(function(err){
	console.log(err);
});

dbclient.updateUser({userId:3,userName:'  ',userStatus : true},function(err){
	console.log(err);
});

dbclient.deleteUser(3,function(err){
	console.log(err);
});

 
업데이트 에 관 해 서 는 몇 가지 업데이트 방식 이 있 습 니 다.
Fields
Name Description
$inc
Increments the value of the field by the specified amount.
$rename
Renames a field.
$setOnInsert
Sets the value of a field upon documentation creation during an upsert. Has no effect on update operations that modify existing documents.
$set
Sets the value of a field in an existing document.
$unset
Removes the specified field from an existing document.
Array
Operators
Name Description
$
Acts as a placeholder to update the first element that matches the query condition in an update.
$addToSet
Adds elements to an existing array only if they do not already exist in the set.
$pop
Removes the first or last item of an array.
$pullAll
Removes multiple values from an array.
$pull
Removes items from an array that match a query statement.
$pushAll
Deprecated. Adds several items to an array.
$push
Adds an item to an array.
Modifiers
Name Description
$each
Modifies the $push and $addToSet operators to append multiple items for array updates.
$slice
Modifies the $push operator to limit the size of updated arrays.
$sort
Modifies the $push operator to reorder documents stored in an array.
Bitwise
Name Description
$bit
Performs bitwise AND and OR updates of integer values.
Isolation
Name Description
$isolated
Modifies behavior of multi-updates to improve the isolation of the operation.
 
 
 

좋은 웹페이지 즐겨찾기