代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了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. SQL SERVER 2008:内部查询处理器错误: 查询处理器在执行过程中遇到意外错误

       今天一个同事突然告诉我,以前跑得很正常的一个SQL语句,执行时突然报如下错误:         消息1222,级别16,状态18,第1 行         已超过了锁请求超时时段.        ...

  2. MySQL Performance-Schema(一) 配置篇

    performance-schema最早在MYSQL 5.5中出现,而现在5.6,5.7中performance-Schema又添加了更多的监控项,统计信息也更丰富,越来越有ORACLE-AWR统计信 ...

  3. JVM之CMS收集器

    CMS(Concurrent Mark Sweep) 最短回收停顿,适合维持响应时间上的要求. 初始标记 Initial mark:标记GC Roots能够关联到的对象.stop-mark. 并发标记 ...

  4. mysql中类似indexOf的方法LOCATE()

     LOCATE(substr,str), LOCATE(substr,str,pos) 第一个语法返回substr在字符串str 的第一个出现的位置. 第二个语法返回子符串 substr 在字符串st ...

  5. mongo学习笔记(一):增删改查

    安装:我是按这篇来弄的 一.Insert 1.db.person.insert({"name":"jack","age":20}) 2.va ...

  6. Unity在Android和iOS中如何调用Native API

    本文主要是对unity中如何在Android和iOS中调用Native API进行介绍. 首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调 ...

  7. spring的路径通配符

    Spring提供了强大的Ant模式通配符匹配,从同一个路径能匹配一批资源. Ant路径通配符支持"?"."*"."**",注意通配符匹配不包 ...

  8. [WPF系列]基础学习(一) WPF是什么?

    引言 学习之前,我们首先大概了解下WPF诞生的背景以及它所能解决的问题或者新颖之处.WPF作为微软新一代的用户界面技术,   WPF简介 WPF的全称是WindowsPresentationFound ...

  9. Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  10. spark dataframe unionall

    今天本来想写一个spark dataframe unionall的demo,由于粗心报下面错误: Exception in thread "main" org.apache.spa ...