Practical Node.js (2018版) 第5章:数据库 使用MongoDB和Mongoose,或者node.js的native驱动。
Persistence with MongoDB and Mongoose
https://github.com/azat-co/practicalnode/blob/master/chapter5/chapter5.md
学习mongodb的官方网站:
https://university.mongodb.com/ (免费课程,-> study guide,-> exam)
https://docs.mongodb.com/manual/tutorial/getting-started/
Mongodb node.js driver3.x版本的 guide:
http://mongodb.github.io/node-mongodb-native/3.1/upgrade-migration/main/
⚠️:本书上的案例使用的是2.2版本的代码,如open命令,在3.0驱动版已经删除。
Mongoose :https://mongoosejs.com/ (17000✨)
写MongoDb Validation, casting和business logic boilerplate是很烦人的drag/boring。
因此使用mongoose代替。
我真的喜欢使用MongoDB with Node。因为这个数据库有JavaScript interface, 并使用JSON-like data structure。
MongoDB是一种NoSQL database。
NoSQL databases (DBs), also called non-relational databases非关系型数据库。
more horizontally scalable,在横向上有更多的扩展性
better suited for distributed systems than traditional SQL,比传统的SQL更适合分布式的系统。
NoSQL DBs内置方法让数据复制和自定义查询语法。这被叫做反规范化denormalization.
NoSQL databases deal routinely with larger data sizes than traditional ones.
通常非关系数据库处理比传统关系数据库更大的数据。
关键的区别:
NoSQL DBs是没有schema的。
没有table,仅仅是一个有IDS的简单store。
大量数据类型是不储存在数据库(没有更多的ALTER table 查询); 它们被移动到app,或者object-relational mapping (ORM) levels--我们的案例,移动到Node.js代码。
作者说:
这些NoSQL最棒的优点!我可以快速地prototype prototyping和iterate (more git pushes!)
一旦我或多或少的完成,我能执行 implement schema和validation in Node.
This workflow allows me to not waste time early in the project lifecycle
while still having the security at a more mature stage.
这种工作流程,让我不会在工程的早期浪费时间,同时在更成熟的阶段仍有安全。?
MongoDB
文档储存的非关系型数据库。
与之对比的是key-value类型的数据库如Redis, 以及wide-column store NoSQL databases。
NoSQL DBs汇总:http://nosql-database.org/
MongoDB是最成熟mature,dependable的NoSQL数据库。
另外,MongoDB有一个JavaScript交互interface!这非常棒!
因为现在无需在前端(browser JavaScript)和后端(Node.js)之间来回切换switch context,和数据库。
这是我最喜欢的功能!
开发MongoDB的公司是一个工业的领导者,提供了学习MongoDB的在线网站 (https://university.mongodb.com).
https://docs.mongodb.com/manual/tutorial/getting-started/
准备开始MongoBD和Node.js , 本章将讲如下章节:
- Easy and proper installation of MongoDB
- How to run the Mongo server
- Data manipulation from the Mongo console
- MongoDB shell in detail
- Minimalistic native MongoDB driver for Node.js example
- Main Mongoskin methods
- Project: Storing Blog data in MongoDB with Mongoskin
Easy and Proper Installation of MongoDB
macOS使用HomeBrew安装。✅
$ brew install mongodb
或者从官网下载文件并配置它。http://www.mongodb.org/downloads
其他安装方式见本文后续说明和连接。
可选:
如果想在你的系统的任何位置使用MongoDB命令, 需要把mongoDB path增加到$PATH变量。
对应macOS,你需要open-system path file, 它在/etc/paths:
$ sudo vi /etc/paths
然后,在/etc/paths文件内增加下面的代码:
/usr/local/mongodb/bin
创建一个data文件夹;默认 MongoDB使用根目录的/data/db。
//创建文件夹
$ sudo mkdir -p /data/db
//改变group和owner
$ sudo chown `id -u` /data/db
这个数据文件夹是你的本地数据库实例的存放位置~,它存放所有databases, documents,和on-all data.
如果你想储存数据在其他地方,可以指定path, 当你登陆你的database实例时,使用--dbpath选项给mongod命令.
各种安装方法官网:
How to Run the Mongo Server
使用mongod命令来启动Mongo server
如果你手动安装,并没有连接位置到PATH, 去到你解包MongoDB的这个folder。那里有个bin文件夹。
输入:
$ ./bin/mongod
如果你像大多数开发者一样,喜欢在你的电脑任意位置输入mongod, 我猜测你把MongoDB bin文件夹放入了你的PATH环境变量中。所以如果你为MongoDB location增加$PATH, 就直接输入:
//任意位置
$ mongod
⚠️注意,在增加一个新的path给$PATH变量需要重启terminal window。
当teminal上出现
waiting for connections on port 27017
意味着MongoDB数据库server正在运行!Congratulations!
默认它监听 http://localhost:27017。
This is the host and port for the scripts and applications to access MongoDB.
In our Node.js code, we use 27017 for for the database and port 3000 for the server.
Data Manipulation from the Mongo Console
和Node.js REPL类似,MongoDB也有一个console/shell,作为database server实例的客户端。
这意味着,保持一个terminal window跑server,再开启一个tab/window用于console。
开启console的命令:
$ ./bin/mongo //或mongo
当你成功地连接到数据库实例,之后你应该看到这些:
MongoDB shell version: 4.0.5
connecting to: test
看到光标>, 现在你在一个不同的环境内而不是zsh或bash。
你不能再执行shell命令了,所以不要使用node server.js或者mkdir。
但是你可以使用JavaScript,Node.js和一些特定的MongoDB代码。
例如,执行下面的命令,来保存一个document{a: 1}, 然后查询这个集合,来看看这个新创建的document:
> db.test.save({a: 1})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5c3c68cb8126284dea64ff02"), "a" : 1 }
命令find(), save()就是字面的意思。
你需要在它们前面加上db.COLLECTION_NAME, 用你自己的名字替代COLLECTION_NAME
⚠️在masOS,使用control + C关闭process,
下章讨论最重要的MongoDB console commands:
MongoDB Console in Detail
MongoDB控制台语法是JavaScript。这非常棒!
最后一件我们想要的是学习一个新的复制的语言,如SQL。
db.test.find() //db是类名 //test是collection name //find是方法名
常用的MongoDB console(shell) commands:
- help: 打印可以使用的命令
- show dbs: 打印数据库名字, prints the names of the databases on the database server to which the console is connected。默认是localhost:27017; 但是如果传递参数到mongo, 我们可以连接到任何远程实例remote instance。
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB- use db_name: switches to db_name
- show collections: 打印所选的数据库中的collections的名单。
- db.collection_name.find(query), 找到所有匹配查询的items。
- db.collection_name.findOne(query)
- ⚠️:find方法是Read Operations(CURD)
- db.collection_name.insert(document) 除此之外还有insertOne, insertMany方法。
- ⚠️:insert是create operations创建操作符
- db.collection_name.save(docuemnt)
- .
- db.collection_name.update(query, {$set: data}): 先找到匹配query条件的items,然后更新。
- ⚠️:update是Update Operations

- db.collection_name.remove(query): 移除所有匹配query的items。
- deleteOne(query), deleteMany(query)是3.0以后的版本的方法。
- printjson(document); 打印变量document的值。
> db.messages.save({a: 1})
> var a = db.messages.findOne()
//a的值是{ "_id" : ObjectId("5c3c8112cdb3fb6e147fb614"), "a" : 1 }
> printjson(a)
> a.text = "hi"
> printjson(a)
> db.messages.save(a)
save()方法有2种工作形式:
如果有_id, 这个document会被更新,无论什么新属性被传入到save()。
> db.messages.save(a)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.messages.find()
{ "_id" : ObjectId("5c3c8112cdb3fb6e147fb614"), "a" : 1, "text" : "hi" }
如果没有_id,将插入一个新document并创建一个新的document ID
Practical Node.js (2018版) 第5章:数据库 使用MongoDB和Mongoose,或者node.js的native驱动。的更多相关文章
- Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose
参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html 参考: mongoose官网(https://mongoosejs.com/docs ...
- Practical Node.js (2018版) 第9章: 使用WebSocket建立实时程序,原生的WebSocket使用介绍,Socket.IO的基本使用介绍。
Real-Time Apps with WebSocket, Socket.IO, and DerbyJS 实时程序的使用变得越来越广泛,如传统的交易,游戏,社交,开发工具DevOps tools, ...
- Practical Node.js (2018版) 第10章:Getting Node.js Apps Production Ready
Getting Node.js Apps Production Ready 部署程序需要知道的方面: Environment variables Express.js in production So ...
- Practical Node.js (2018版) 第3章:测试/Mocha.js, Chai.js, Expect.js
TDD and BDD for Node.js with Mocha TDD测试驱动开发.自动测试代码. BDD: behavior-driven development行为驱动开发,基于TDD.一种 ...
- Practical Node.js (2018版) 第8章:Building Node.js REST API Servers
Building Node.js REST API Servers with Express.js and Hapi Modern-day web developers use an architec ...
- Practical Node.js (2018版) 第4章: 模版引擎
Template Engines: Pug and Handlebars 一个模版引擎是一个库或框架.它用一些rules/languages来解释data和渲染views. web app中,view ...
- Practical Node.js (2018版) 13章, Node HTTP/2 Servers
新增的章节. If you are not using HTTP/2, then you are losing out on big improvements. HTTP/2相比http/1有很大的区 ...
- Practical Node.js (2018版) 14章, async code in Node
Asynchronous Code in Node 历史上,Node开发者只能用回调和事件emitters. 现在可以使用一些异步的语法: async module Promises Async/aw ...
- Practical Node.js摘录(2018版)第1,2章。
大神的node书,免费 视频:https://node.university/courses/short-lectures/lectures/3949510 另一本书:全栈JavaScript,学习b ...
随机推荐
- POJ 2018 Best Cow Fences(二分最大区间平均数)题解
题意:给出长度>=f的最大连续区间平均数 思路:二分这个平均数,然后O(n)判断是否可行,再调整l,r.判断方法是,先求出每个数对这个平均数的贡献,再求出长度>=f的最大贡献的区间,如果这 ...
- CentOS7设置定时任务 每隔30分钟执行一次命令
ref https://blog.csdn.net/xiangxianghehe/article/details/78149094 一.安装 crontabs服务并设置开机自启: yum inst ...
- AtCoder Beginner Contest 117 解题报告
果然abc都是手速场. 倒序开的qwq. D题因为忘记1e12二进制几位上界爆了一发. A - Entrance Examination 就是除一下就行了... 看样例猜题意系列. #include& ...
- 集合00_Java集合框架
集合类概述 1.继承树 2.集合和数组 区别如下: 数组可以存储基本数据类型,也可以存储引用类型:而集合只能存储引用类型(比如存储int,它会自动装箱成Integer) 数组长度固定,集合长度可变 3 ...
- mybatis动态传入表名、列名
原文:http://luoyu-ds.iteye.com/blog/1517607 要实现动态传入表名.列名,需要做如下修改 添加属性statementType=”STATEMENT” (可省略) 同 ...
- [UVA-11039]Children's Game
解析 微扰法贪心经典题 代码 #include <bits/stdc++.h> using namespace std; bool cmp(const string &x, con ...
- javascript知识体系
JAVASCRIPT 篇 0.基础语法 javascript基础语法包括:变量定义.数据类型.循环.选择.内置对象等. 数据类型有string,number,boolean,null,undefine ...
- git core.autocrlf配置 解决Windows和Linux(Mac)换行问题
格式化 格式化是许多开发人员在协作时,特别是在跨平台情况下,遇到的令人头疼的细小问题. 由于编辑器的不同或者Windows程序员在跨平台项目中的文件行尾加入了回车换行符, 一些细微的空格变化会不经意地 ...
- Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext
由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包. 结果在运行测试用 ...
- 网页中Div的打印
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...