当前位置:网站首页>Nodejs operation mongodb
Nodejs operation mongodb
2022-07-28 07:20:00 【▀】
function Mongo(options) {
this.settings = {
url: 'mongodb://127.0.0.1:27017/student',
MongoClient: require('mongodb').MongoClient,
assert: require('assert')
};
for (let i in options) {
this.settings[i].url = options[i].url;
}
this._run = function (fun) {
let settings = this.settings;
this.settings.MongoClient.connect(this.settings.url, function (err, db) {
settings.assert.equal(null, err);
fun(db, function () {
db.close();
});
});
};
/**
* Insert a piece of data
* Insert the name of the table
* Matching condition
* Callback function
*/
this.insertOne = function (collectionName, data, func) {
let insertDocuments = function (db, callback) {
let collection = db.collection(collectionName);
collection.insertOne([
data
], function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(insertDocuments);
};
/**
* Insert multiple data
* Insert the name of the table
* Matching condition
* Callback function
*/
this.insertMany = function (collectionName, data, func) {
let insertDocuments = function (db, callback) {
let collection = db.collection(collectionName);
collection.insertMany([
data
], function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(insertDocuments);
};
/**
* Update a piece of data
* The name of the table
* Matching condition
* Callback function
*/
this.updateOne = function (collectionName, updateData, data, func) {
// Update data
let updateDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.updateOne(updateData
, { $set: data }, function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(updateDocument);
};
/**
* Update multiple data
* Collection name
* Update data
* Matching condition
* Callback function
*/
this.updateMany = function (collectionName, updateData, data, func) {
// Update data
let updateDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.updateMany(updateData
, { $set: data }, function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(updateDocument);
};
/**
* Delete a piece of data
* The name of the table
* Matching condition
* Callback function
*/
this.deleteOne = function (collectionName, data, func) {
// Delete data
let deleteDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.deleteOne(data, function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(deleteDocument);
};
/**
* Delete multiple data
* The name of the table
* Matching condition
* Callback function
*/
this.deleteMany = function (collectionName, data, func) {
// Delete data
let deleteDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.deleteMany(data, function (err, result) {
if (!err) {
func(true);
} else {
func(false);
}
callback(result);
});
};
this._run(deleteDocument);
};
/**
* Query the document
* The name of the table
* Matching condition
* Callback function
*/
this.find = function (collectionName, data, func) {
// Find data
let findDocuments = function (db, callback) {
// Get the documents collection
let collection = db.collection(collectionName);
// Find some documents
collection.find(data).toArray(function (err, docs) {
if (!err) {
func(true, docs);
}
else {
func(false, err);
}
callback(docs);
});
};
this._run(findDocuments);
};
/**
* Paging query
* Collection name
* Matching condition
* Sort field { type: 1 } // Press type Fields in ascending order { type: -1 } // Press type Fields in descending order
* Offset
* Number of queries
* Callback function
*/
this.findsortpage = function (collectionName, data, _sortcoll, _sortseek/* Offset */, _sortnum/* Number of queries */, func) {
// Find data
let findDocuments = function (db, callback) {
// Get the documents collection
let collection = db.collection(collectionName);
// Find some documents
collection.find(data).sort(_sortcoll).skip(parseInt(_sortseek)).limit(parseInt(_sortnum)).toArray(function (err, docs) {
if (!err) {
func(true, docs);
}
else {
func(false, err);
}
callback(docs);
});
};
this._run(findDocuments);
};
/**
* Left connection
* Collection name
* Right set
* Left set join Field
* Right set join Field
* Newly generated field ( type array)
* Callback function
*/
this.leftjoinon = function (collectionName, rightcollname, leftjoinid, rightjoinid, newarrayname, func) {
// Update data
let updateDocument = function (db, callback) {
let collection = db.collection(collectionName);
collection.aggregate([
{
$lookup:
{
from: rightcollname, // Right set
localField: leftjoinid, // Left set join Field
foreignField: rightjoinid, // Right set join Field
as: newarrayname // Newly generated field ( type array)
}
}
]).toArray(function (err, res) {
if (!err) {
func(true, res);
}
else {
func(false, err);
}
callback(res);
});
};
this._run(updateDocument);
};
}
module.exports = Mongo;
边栏推荐
- Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0
- Gobang optimized version
- guava之EventBus
- Easypoi export table with echars chart
- 分布式系分发展概览
- Freemaker merges cells, uses if and else tags, and processes null and empty strings
- Log in to heroku and the solution of IP address mismatch appears
- Add, delete, check and modify linked lists
- MHA高可用配置及故障切换
- guava之限流RateLimiter
猜你喜欢

vcf文件制作

GFS分布式文件系统

Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0

Joern's code uses -devign

kali下安装nessus

Easypoi export table with echars chart

Understanding of maximum likelihood estimation, gradient descent, linear regression and logistic regression

远程访问云服务器上Neo4j等服务的本地网址

win下安装nessus

metasploit渗透ms7_010练习
随机推荐
map使用tuple实现多value值
MySQL字段 不推荐使用 Null 的理由
Addition, deletion, check and modification of sequence table
远程访问云服务器上Neo4j等服务的本地网址
win下安装nessus
MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类
guava之Retryer
C language push box
Pytorch extracts the feature map of a certain layer
NAT network address translation
Database-Trivial
GFS分布式文件系统
Two horizontal and vertical screen switching schemes for uniapp mobile terminal
Standard C language learning summary 8
Standard C language summary 1
Eslint FAQ solutions collection
shell---sed语句练习
高性能内存队列-Disruptor
Branch and loop statements
rsync+inotify实现远程实时同步