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 ...
随机推荐
- Lucene索引文件学习
最近在做搜索,抽空看一下lucene,资料挺多的,不过大部分都是3.x了--在对着官方文档大概看一下. 优化后的lucene索引文件(4.9.0) 一.段文件 1.段文件:segments_5p和s ...
- 【转】Hive内部表、外部表
hive内部表.外部表区别自不用说,可实际用的时候还是要小心. 1. 内部表: create table tt (name string , age string) location '/input/ ...
- 大数据系列(4)——Hadoop集群VSFTP和SecureCRT安装配置
前言 经过前三篇文章的介绍,已经通过VMware安装了Hadoop的集群环境,当然,我相信安装的过程肯定遇到或多或少的问题,这些都需要自己解决,解决的过程就是学习的过程,本篇的来介绍几个Hadoop环 ...
- vs与qt配置
1.error: these Qt version are inaccessible;Qt5.4.0 in D:\qt5.4.0\5.4\msvc2013Make sure that you have ...
- Android资源(图片)命名规范
(转自:http://www.jb51.net/article/38796.htm) 图片命名注意: 1,不能以下划线("_")开头: 2,以数字加下划线("[0-9]_ ...
- 理解 Node.js 里的 process.nextTick()
有很多人对Node.js里process.nextTick()的用法感到不理解,下面我们就来看一下process.nextTick()到底是什么,该如何使用. Node.js是单线程的,除了系统IO之 ...
- 利用oneproxy部署mysql数据库的读写分离
实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验说明:本实验共有4台主机,IP分配如拓扑 实验软件:mariadb-10.0.20 oneproxy-rhel ...
- makefile 学习笔记
1/ 编写简单makefile test_out: test.o g++ test.o -o test_out test.o: test.cpp test.h g++ -c test.cpp test ...
- required string parameter XXX is not present
@RequestParam jQuery调用方式: deleteFile: function(filePath) { return ajax({ method: 'POST', url: '/cm/s ...
- Java集合框架之map
Java集合框架之map. Map的主要实现类有HashMap,LinkedHashMap,TreeMap,等等.具体可参阅API文档. 其中HashMap是无序排序. LinkedHashMap是自 ...