NodeJs + mongodb模块demo
代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了nodejs中异步的一个小坑。未来的坑还有很多,慢慢找坑填坑吧。
参考资料如下:
1、断言模块 : https://nodejs.org/api/assert.html
2、mongodb模块:https://github.com/mongodb/node-mongodb-native
废话不多说了,发完代码睡觉了,有兴趣的朋友可以持续关注本系列。
//加载nodejs中的mongodb模块
var MongoClient = require('mongodb').MongoClient; //加载assert(断言模块)参考地址:https://nodejs.org/api/assert.html
var assert = require('assert'); // mongodb HOST地址 test表示当前所在的数据库
var url = 'mongodb://localhost:27017/test';
//启动mongodb服务,建立连接
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server"); //同步嵌套的写法
insertDocuments(db, function() {
updateDocument(db, function() {
deleteDocument(db, function() {
findDocuments(db, function() {
db.close();
});
});
});
}); //仔细观察同步与异步的CURD执行顺序。尝试在异步后添加db.close(); 思考为什么报错。
//异步的写法
// insertDocuments(db, function(){});
// updateDocument(db, function(){});
// deleteDocument(db, function(){});
// findDocuments(db, function(){}); }); //下面演示CURD的操作
var insertDocuments = function(db, callback) {
//得到文档集合
var collection = db.collection('rover');
//构造数据
var testData = [{a:1},{a:2},{a:3}];
//插入数据
collection.insertMany(testData, function(err, result) {
assert.equal(err, null);
assert.equal(3,result.result.n);
assert.equal(3,result.ops.length);
console.log('Inserted 3 Documents into the document collections');
callback(result); }); }; //Updating a Documents 修改操作
var updateDocument = function(db, callback) {
//得到文档集合
var collection = db.collection('rover');
//修改文档集合
var update = {a:2};
var change = {$set:{a:5555}};
collection.updateOne(update,change, function(err, result) {
assert.equal(err,null);
assert.equal(1, result.result.n);
console.log("Updated the document with the field a equal to 2");
callback(result);
})
}; //Delete a document
var deleteDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('rover');
// Insert some documents
collection.deleteOne({ a : 3 }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Removed the document with the field a equal to 3");
callback(result);
});
}; //读取数据
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('rover');
// Find some documents
collection.find({}).toArray(function(err, docs) {
// assert.equal(err, null);
// assert.equal(2, docs.length);
console.log("Found the following records");
console.dir(docs);
callback(docs);
});
};
NodeJs + mongodb模块demo的更多相关文章
- 大熊君大话NodeJS之------MongoDB模块(额外篇)
一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为 ...
- NodeJS学习笔记之MongoDB模块
其中还有,nodejs远程连接mysql数据库 一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于 ...
- react+react-router+react-redux+nodejs+mongodb项目
一个实际项目(OA系统)中的部分功能.这个demo中引入了数据库,数据库使用了mongodb.安装mongodb才能运行完整的功能.要看完整的项目可以移步我的github 技术栈 React v15. ...
- Nodejs+MongoDB+Bootstrap+esj搭建的个人简易博客
github:https://github.com/yehuimmd/myNodeBloy Nodejs+MongoDB+jQuery+Bootstrap-esj搭建的个人简易博客 主要功能 前台 : ...
- nodejs cluster模块初探
大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...
- nodejs事件模块
nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...
- 配置 Windows 下的 nodejs C++ 模块编译环境
根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...
- NodeJS http 模块
#4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...
- nodejs的模块系统(实例分析exprots和module.exprots)
前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...
随机推荐
- ORA-01113: file xxxx needs media recovery
由于规范存储位置以及存储空间调整缘故需要移动表空间MRP_INDEX2的数据文件,如下所示,首先将表空间MRP_INDEX2脱机; 然后复制数据文件:接着重命名数据文件,最后将表空间MRP_INDEX ...
- x01.Weiqi.10: 死活问题
估计得不错,点目后,仅一个方法:UpdateMeshes5() 就完美解决了梅花六.刀把五.斗笠四.盘角曲四等死活问题.先来看看效果图: 其代码如下: void UpdateMeshes5(bool ...
- x01.os.18: MBR
硬盘不同于软盘,它是要分区的.这时,mbr(master boot record)便不可少了.安装 os 硬盘的第一扇区,开始有一小段不多于 446 字节的程序,然后是分区表 512-446-2 字节 ...
- iteye上总结的编程精华资源
原文:http://www.iteye.com/magazines/130 博客是记录学习历程.分享经验的最佳平台,多年以来,各路技术大牛在ITeye网站上产生了大量优质的技术文章,并将系列文章集结成 ...
- QT210 android2.3 和android4.0 烧写编译日记
QT210下载烧录编译android2.3过程 工作环境:ubuntu12.04.5 | QT210开发板光盘 | QT210开发板 android2.3编译环境:gcc version 4.4.7 ...
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- github
学习github的不错的资源 http://gitref.org/zh/index.html https://wuyuans.com/2012/05/github-simple-tutorial/#t ...
- Struts2文件上传和文件下载
一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...
- [备份]破解Xamarin
[转]试用了一阵子Mono For Android,今天到期了,,囊中羞涩,只好破解. 说是要在vs2013的英文界面下运行破解包,不知道是真是假,下载并安装了一个. 然后又下载了破解包.是个名为xa ...
- python高级之操作数据库
python高级之操作数据库 本节内容 pymysql介绍及安装 使用pymysql执行sql 获取新建数据自增ID fetch数据类型设置 1.pymysql介绍及安装 在python2中连接数据库 ...