Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
132
|
第
13
章
于免费(沙箱)账号来说,基本上怎么选都没关系,不过你应该找距离比较近的数据中心
(然而并非每个数据中心都支持沙箱账号)。选择
SANDBOX
,并选择一个地区
,然后选择
一个数据库名,点击“
Submit
Order
”(即使它是免费的,也会有一个订单)。页面将跳转
到你的数据库列表,几秒之后,你的数据库就可以使用了。
建立数据库只是完成了一半工作,还要知道如何在
Node
中访问它。这便是
Mongoose
发挥
作用的地方了。
13.3.4
 
Mongoose
虽然
MongoDB
有比较底层的驱动可以使用,但很可能你还是希望使用一个对象文档映射
(
ODM
)
。适合
MongoDB
的主流
ODM
就是
Mongoose
。
JavaScript
的一个优势是其对象模型极其灵活
。你如果想给一个对象增加属性或方法,只
管去做,无须担心会因此而修改对象所属的类。不幸的是,这种“随心所欲”会对数据库
产生负面影响,数据会因此变得零散,难以优化。因此,
Mongoose
选择通过引入
模式
和
模型
(两者结合起来,就类似于传统的面向对象编程中的类)达成一个平衡。它的模式是
灵活的,但仍然可以为你的数据库提供必要的结构。
在开始之前,要先安装
Mongoose
模块:
npm install mongoose
然后把数据库的认证信息加入名为
.credentials.development.json
的文件中:
"mongo": {
"connectionString": "your_dev_connection_string"
}
你可以在
mLab ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099