代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了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. centos为用户增加ssh key

    linux增加用户,为用户增加key 可以用  ssh-keygen -t rsa 添加ssh的key,会得到public_key和自己的private_key 然后这个key可以用在任何用户上 ad ...

  2. 聊下 git 使用前的一些注意事项

    连接方式https.ssh 在使用git的时候,不管你的服务器是开源平台github还是私服gitlab,你都需要clone仓库到本地,这个clone的时候就需要你选择连接方式.这个连接方式决定了你与 ...

  3. C# 实现酒店房态图

    酒店管理系统最重要和实用的是能够及时.一目了然的反应房间状态的房态图,之前在开发一个的酒店管理系统中做了一个还算实用的房态图,现在分享下: 鼠标移到每个房间上面,可显示提示信息: 还可以自定义设置不同 ...

  4. 【Linux学习】Linux下用户组、文件权限详解

    原文地址:http://www.cnblogs.com/123-/p/4189072.html Linux下用户组.文件权限详解 用户组 在linux中的每个用户必须属于一个组,不能独立于组外.在li ...

  5. 理解 Keystone 核心概念 - 每天5分钟玩转 OpenStack(18)

    作为 OpenStack 的基础支持服务,Keystone 做下面这几件事情: 管理用户及其权限 维护 OpenStack Services 的 Endpoint Authentication(认证) ...

  6. WPF 提示框、确认框、确认输入框

    1.提示框 分为提示.异常.失败.成功几种类型 方法: /// <summary> /// 弹出提示 /// 标题:提示 /// </summary> /// <para ...

  7. 深入理解Thread.sleep的含义

    转载一篇对sleep说的非常好的一篇文章,原文http://www.cnblogs.com/ILove/archive/2008/04/07/1140419.html 我们可能经常会用到 Thread ...

  8. mysqld设置密码

    用root 进入mysql后 mysql>set password =password('你的密码'); mysql>flush privileges;   登录: mysql -u ro ...

  9. 漫谈c++11 Thread库之使写多线程程序

    c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...

  10. 识别 Linux上的设备(磁盘)类型

    1. Linux 上的设备 (device) Linux 操作系统中,各种设备驱动(device driver)通过设备控制器(device controller)来管理各种设备(device),其关 ...