当前位置: 首页 > 编程日记 > 正文

mongoDB数据库操作工具库

/*
Mongodb的数据库工具类
*/
var client = require('mongodb').MongoClient;function MongoUtil() {
this.url="mongodb://localhost:27017/storage";//在本地新建数据库storage,此后插入的数据都在storage中
}MongoUtil.prototype.connect=function(callback){
var that = this;
// console.log(3);
client.connect(this.url,function(err,db){
// console.log(4);
if(err){
console.dir(err);
}else{
that.db = db;
callback();
}
});
}MongoUtil.prototype.close = function(){
this.db.close();
}MongoUtil.prototype.insertDocuments = function(collectionName,docs,callback) {
var that = this;
this.connect(function(){
var collection = that.db.collection(collectionName);
collection.insertMany(docs, function(err,result){
if(err){
console.dir(err);
}else{
callback(result);
}
that.close();
});});
}MongoUtil.prototype.insertDocument = function(collectionName,doc,callback) {
var that = this;
this.connect(function(){
var collection = that.db.collection(collectionName);
collection.insertOne(doc, function(err,result){
if(err){
console.dir(err);
}else{
callback(result.insertedCount);
}
that.close();
});
});
}MongoUtil.prototype.findAllDocuments = function(collectionName, callback) {
var that = this;
this.connect(function(){
var collection = that.db.collection(collectionName); 
collection.find({}).toArray(function(err,result){
if(err){
console.dir(err);
}else{
callback(result);//返回插入的行数
}
that.close();
});
})
}MongoUtil.prototype.update = function(collectionName,filter,update,callback){
var that = this;
this.connect(function(){
var collection = that.db.collection(collectionName);
collection.updateOne(filter,update,function(err,result){
if(err){
console.dir(err);
}else{
callback(result.insertedCount);//返回插入的行数
}
});
});
}MongoUtil.prototype.findOne = function(collectionName,query,options,callback){
var that = this;
this.connect(function(){
var collection = that.db.collection(collectionName);
collection.findOne(query,options).then(function(doc){
callback(doc);
});
});
}module.exports=new MongoUtil();

转载于:https://www.cnblogs.com/cy2525/p/6506420.html

相关文章:

开源许可证 如何工作_开源许可证的工作方式以及如何将其添加到您的项目中...

开源许可证 如何工作by Radu Raicea由Radu Raicea 开源许可证的工作方式以及如何将其添加到您的项目中 (How open source licenses work and how to add them to your projects) Recently, there was some exciting news for developers around the world. Facebook changed t…

通过API文档查询Math类的方法,打印出近似圆,只要给定不同半径,圆的大小就会随之发生改变...

package question;import java.util.Scanner; import java.lang.Math;public class MathTest {/*** 未搞懂* param args*/public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("请输入圆的半径:");Scanner in new Scanne…

4 OC 中的内存分配以及内存对齐

目录 一 OC 中的内存分配 一 OC 中的内存分配 student 结构体明明是20?为什么是24个字节,因为结构体会按照本身成员变量最大的内存进行对齐,最大成员变量是8个字节,因此就是8的倍数,24个字节。 class_getInstanc…

JDE函数--GetUDC(B函数)

GetUDC使用方式: 转载于:https://www.cnblogs.com/GYoungBean/p/4117965.html

k8s crd构建方法_告诉您正在构建没人想要的东西的8种方法(以及处理方法)

k8s crd构建方法by Geoffrey Bourne杰弗里伯恩(Geoffrey Bourne) 告诉您正在构建没人想要的东西的8种方法(以及处理方法) (8 ways to tell you’re building something nobody wants (and what to do about it)) Building something users want is hard — damn hard. They ar…

iOS开发 - 线程与进程的认识与理解

进程: 进程是指在系统中正在运行的一个应用程序,比如同时打开微信和Xcode,系统会分别启动2个进程;每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内;线程: 一个进程要想执行任务,必须得有…

Winform开发中常见界面的DevExpress处理操作

我们在开发Winform程序的时候,需要经常性的对界面的一些控件进行初始化,或者经常简单的封装,以方便我们在界面设计过程中反复使用。本文主要介绍在我的一些项目中经常性的界面处理操作和代码,以便为大家开发的时候提供必要的参考。…

5 OC 中的三种对象

目录 OC 中对象的分类 一 instance 对象 二 类对象 三 元类对象 总结: OC 中对象的分类 instance 对象 类对象 元类对象 一 instance 对象 内存中包含哪些信息 isa 指针 其他成员的变量Student *stu1 [[Student alloc]init]; 以上的stu1 就是实例对象 二 类对象 以…

travis ci_如何使用Travis CI和GitHub进行Web开发工作流程

travis ciby Vijayabharathi Balasubramanian通过Vijayabharathi Balasubramanian 如何使用Travis CI和GitHub进行Web开发工作流程 (How to use Travis CI and GitHub for your web development workflow’s heavy lifting) It’s common to hack together apps on CodePen wh…

android.view.ViewRoot$CalledFromWrongThreadException的解决办法

android 是不允许子线程直接更新UI的,如果一定要在子线程直接更新UI就会出现android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.大概意思就是说 只有原来创建找个视图hierarchy的…

6 OC中 isa 和 superclass 的总结

目录 一 关于isa 和 superclass 的总结 二 为什么基类的metaclass 的superclass 指向的是基类的类 三 isa 的细节问题 总结如下: instance 的isa 指向是classclass 的isa 指向是metaclassmetaclass 的isa指向是基类的imetaclassclass 的superclass 指向的是父类…

opencv下指定文件夹下的图片灰度化(图片的读取与保存)-------简单记录

对于此功能其实很简单&#xff1a;主要是在c方面的字母数字的拼接问题存在一定的问题。C数字字母拼接问题&#xff1a; 1 #include <fstream> 2 #include <string> 3 #include <iostream> 4 #include "highgui.h" 5 #include <cv.h> 6 #…

css菜单缓慢滑动_如何使用HTML,CSS和JavaScript构建滑动菜单栏

css菜单缓慢滑动by Supriya Shashivasan由Supriya Shashivasan 如何使用HTML&#xff0c;CSS和JavaScript构建滑动菜单栏 (How to build a sliding menu bar using HTML, CSS and JavaScript) A menu is what you look for when you land at a website. It has options and gi…

素数环问题---深度搜索遍历

1264: 素数环 时间限制: 1 Sec 内存限制: 128 MB提交: 29 解决: 8[提交][状态][讨论版]题目描述 有一个长度为n的环形序列由1,2,3,...,n组成&#xff0c;环中相邻两个整数和均为素数。你需要找到所有满足条件的环。输入 输入n表示环的长度&#xff08;n<16&#xff09;输出…

android之Notification通知

我们在用手机的时候&#xff0c;如果来了短信&#xff0c;而我们没有点击查看的话&#xff0c;是不是在手机的最上边的状态栏里有一个短信的小图标提示啊&#xff1f;你是不是也想实现这种功能呢&#xff1f;今天的Notification就是解决这个问题的。 package cn.com.chenzheng_…

7 OC 中class 类的结构

目录 一 OC 中class 的结构 https://opensource.apple.com/tarballs/objc4/ 在最新的objc源码中 化繁就简来看的话 是以下结构 struct objc_class : objc_object {objc_class(const objc_class&) delete;objc_class(objc_class&&) delete;void operator(con…

apple id无法创建_我们如何使用Apple的学习框架来创建我们的第一个应用程序

apple id无法创建by Jonata Corra由JonataCorra 我们如何使用Apple的学习框架来创建我们的第一个应用程序 (How we used Apple’s learning framework to create our first app) After one month of work, my team and I finished the first version of Echo, our tracker iOS…

个人作业1:小学四则运算——基于控制台

a.需求分析&#xff1a; 自动生成小学四则运算题目的命令行 “软件”&#xff0c;满足以下需求&#xff1a; 除了整数以外&#xff0c;还要支持真分数的四则运算&#xff0c;真分数的运算&#xff0c;例如&#xff1a;1/6 1/8 7/24运算符为 , −, , 并且要求能处理用户…

getchar返回int类型

#include <stdio.h> /* copy input to output; 2nd version */main(){int c;c getchar();while(c ! EOF){putchar(c);c getchar();}} 直觉告诉我getchar返回值应该是char类型的&#xff0c;这个地方为什么不能用char类型来存储getchar()的返回值呢&#xff1f; 其实文中…

8 iOS中KVO 的本质

前言本质 Automatic key-value observing is implemented using a technique called isa-swizzling 这计划的意思就是 自动的键值观察的实现基于 isa-swizzling 原理 1.KVO是基于runtime机制实现的 2.当某个类的属性对象第一次被观察时&#xff0c;系统就会在运行期动态地创…

完成工作表-使用Google Spreadsheets作为数据后端

by Gilad Dayagi通过吉拉德达亚吉 完成工作表-使用Google Spreadsheets作为数据后端 (Get Sheet Done — using Google Spreadsheets as your data backend) If you want to rapidly prototype your next web apps, try using Google Spreadsheets as your data backend.如果您…

BIEE-CSS样式大全

字体属性&#xff1a;(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到&#xff0c;只要用数值就可以&#xff0c;单位&#xff1a;PX、PD 样式 {font-style: oblique;}(偏斜体) italic;(斜体) normal;(正常) 行高 {line-height: normal;}(正常) 单位&…

基于verilog的FPGA编程经验总结(XILINX ISE工具)

1.用ISE仿真的时候.所用变量一定要初始化. ISE默认初始量为"XXXXX", 而Quarters是默认为"00000"的, 其实实际上, 下到FPGA里后也是默认为0的,只是可以说ISE严谨得令人DT吧.比如说用一个累加器, result ABresult ,必须保证在某一刻A, B, result都为定值时,…

6 OC 中的isa 指针

目录 一 isa 指针 二 类对象中的superclass 一 isa 指针 isa 指针 &#xff0c;OC 中的对象都是有的 如下图所示&#xff0c;实例对象isa 指针指向 类对象&#xff0c;类对象的isa 指针指向 元类对象 二 类对象中的superclass superclass 有什么用呢&#xff1f; 比如说创…

btf-raft共识算法_了解Raft共识算法:学术文章摘要

btf-raft共识算法by Shubheksha通过Shubheksha 了解Raft共识算法&#xff1a;学术文章摘要 (Understanding the Raft consensus algorithm: an academic article summary) This post summarizes the Raft consensus algorithm presented in the paper In Search of An Underst…

iOS asset 中定义颜色,xib中便捷访问

在aseet 中定义一个颜色 这样就可以在xib 中访问颜色了&#xff0c;这样就不用重复的去输入

三种序列化方式性能比较

一下代码比较了二进制序列化、xml序列化、Protobuf序列化的运行时间&#xff0c;可是代码显得十分冗余&#xff0c;是否有大神可以指点一二&#xff0c;万分感谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; usi…

mac mini 装UBUNTU后没有WIFI解决办法

1、在终端中运行如下命令&#xff0c;重新安装b43相关的全部驱动和firmware: 复制代码代码如下:sudo apt-get install bcmwl-kernel-source #Broadcom 802.11 Linux STA 无线驱动源sudo apt-get install broadcom-sta-commonsudo apt-get install broadcom-sta-sourcesudo apt-…

区块链c端应用小程序_区块链如何真正起作用? 我建立了一个应用程序向您展示。...

区块链c端应用小程序by Sean Han通过肖恩韩 区块链如何真正起作用&#xff1f; 我建立了一个应用程序向您展示。 (How does blockchain really work? I built an app to show you.) According to Wikipedia, a blockchain is:根据维基百科&#xff0c;一个区块链是&#xff1…

HDU 4913 Least common multiple

/* hdu4913 Least common multiple http://acm.hdu.edu.cn/showproblem.php?pid4913 离散化 线段树 统计逆序数思想 tips: 1、线段树中一定要到处都取模&#xff0c;否则wa。。。 2、lazy是乘积的形式出现&#xff0c;不是加和*/ #include <cstdio> #include <algori…