Nodejs访问MongoDB

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();

 假设已经存在UserInfo集合。

 

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

NameDescription
$incIncrements the value of the field by the specified amount.
$renameRenames a field.
$setOnInsertSets the value of a field upon documentation creation during an upsert. Has no effect on update operations that modify existing documents.
$setSets the value of a field in an existing document.
$unsetRemoves the specified field from an existing document.

Array

Operators
NameDescription
$Acts as a placeholder to update the first element that matches the query condition in an update.
$addToSetAdds elements to an existing array only if they do not already exist in the set.
$popRemoves the first or last item of an array.
$pullAllRemoves multiple values from an array.
$pullRemoves items from an array that match a query statement.
$pushAll Deprecated. Adds several items to an array.
$pushAdds an item to an array.
Modifiers
NameDescription
$eachModifies the $push and $addToSet operators to append multiple items for array updates.
$sliceModifies the $push operator to limit the size of updated arrays.
$sortModifies the $push operator to reorder documents stored in an array.

Bitwise

NameDescription
$bitPerforms bitwise AND and OR updates of integer values.

Isolation

NameDescription
$isolatedModifies behavior of multi-updates to improve the isolation of the operation.
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值