代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了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的更多相关文章

  1. 大熊君大话NodeJS之------MongoDB模块(额外篇)

    一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为 ...

  2. NodeJS学习笔记之MongoDB模块

    其中还有,nodejs远程连接mysql数据库 一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于 ...

  3. react+react-router+react-redux+nodejs+mongodb项目

    一个实际项目(OA系统)中的部分功能.这个demo中引入了数据库,数据库使用了mongodb.安装mongodb才能运行完整的功能.要看完整的项目可以移步我的github 技术栈 React v15. ...

  4. Nodejs+MongoDB+Bootstrap+esj搭建的个人简易博客

    github:https://github.com/yehuimmd/myNodeBloy Nodejs+MongoDB+jQuery+Bootstrap-esj搭建的个人简易博客 主要功能 前台 : ...

  5. nodejs cluster模块初探

    大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,为了使用多核cpu来提高性能 就有了cluster,让node可以利用多核CPU实现并行 ...

  6. nodejs事件模块

    nodejs 事件模块 events 只有一个对象 EventEmitter . var EventEmitter = require('events').EventEmitter;var life ...

  7. 配置 Windows 下的 nodejs C++ 模块编译环境

    根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境". 所有需要的安装文件, 我都下载好放到百度云盘了: nod ...

  8. NodeJS http 模块

    #4 NodeJS http 模块 工作目录 server.js var http = require('http'); var fs = require('fs'); var path = requ ...

  9. nodejs的模块系统(实例分析exprots和module.exprots)

    前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...

随机推荐

  1. ORA-04063: view "SYS.DBA_REGISTRY" has errors

    测试环境做了RMAN还原(从10.2.0.4.0 32bit 还原到 10.2.0.4.0 64bit)后,查询dba_registry系统视图时报如下错误 SQL> select  comp_ ...

  2. Cannot create an instance of OLE DB provider "OraOLEDB.Oracle" for linked server "xxxxxxx".

    在SQL SERVER 2008 R2下用Windows 身份认证的登录名创建了一个访问ORACLE数据库的链接服务器xxxxx,测试成功,木有问题,但是其它登录名使用该链接服务器时,报如下错误: 消 ...

  3. redis 集群配置实战

    文章转载自:http://hot66hot.iteye.com/blog/2050676 最近研究Redis-cluster,正好搭建了一个环境,遇到了很多坑,系统的总结下,等到redis3 rele ...

  4. 关于linux,我们应该学什么?

    为了系统地学习一下linux相关知识,我花了三天的时间看完了那本经典的<鸟哥的linux私房菜>.所谓的经典必然是有其过人之处,而看完这本书之后,我毫无疑问地加入了强烈推荐这本书的行列. ...

  5. Spark官方文档 - 中文翻译

    Spark官方文档 - 中文翻译 Spark版本:1.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 引入Spark(Linki ...

  6. hive建表与数据的导入导出

    建表: create EXTERNAL table tabtext(IMSI string,MDN string,MEID string,NAI string,DestinationIP string ...

  7. saltstack简单部署和实践

    #Elaine:master;Dylan:minion#利用salt给minion安装包[root@elaine states]# vim /etc/salt/master default_inclu ...

  8. Android 实现网络多线程APK文件下载

    (转自:http://blog.csdn.net/mad1989/article/details/38421465) 实现原理 (1)首先获得下载文件的长度,然后设置本地文件的长度. (2)根据文件长 ...

  9. 三维等值面提取算法(Dual Contouring)

    上一篇介绍了Marching Cubes算法,Marching Cubes算法是三维重建算法中的经典算法,算法主要思想是检测与等值面相交的体素单元并计算交点的坐标,然后对不同的相交情况利用查找表在体素 ...

  10. hibernate inverse属性的作用

    hibernate配置文件中有这么一个属性inverse,它是用来指定关联的控制方的.inverse属性默认是false,若为false,则关联由自己控制,若为true,则关联由对方控制.见例子: 一 ...