Nodejs MongoDB 방문
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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
레코드를 업데이트하고 업데이트 전에 동일한 레코드를 삭제하는 방법(nest js & mongoDB)ID로 레코드를 업데이트하고 싶지만 업데이트 전에 동일한 레코드에 이전에 저장된 데이터를 삭제하고 싶습니다. 프로세스는 무엇입니까? 컨트롤러.ts 서비스.ts 나는 이것을 해결하기 위해 이런 식으로 노력하고 있습니다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.