1,  在schema 目录创建 users.js 文件,通过 mongoose 模块来操作数据库

2,  在定义 users 表结构之前,需要让应用支持或连接数据库 。 所以要在应用的入口文件 app.js 中 进行数据库的重放(即加载数据库)

3 ,    var mongoose = require('mongoose')

4 ,    mongoose.connect()  //连接数据库  。

5  ,   在连接之前需要开启数据库 ,

 (1)    cmd 进入  E:\MongoDB\Server\4.0\bin    目录下  E:\MongoDB\Server\4.0\bin> mongod       (执行前需要设置参数,详情看1.5.2,1.5.3)

(2) 指定数据库存储路径 : 项目的数据库目录  。

(3)    指定端口

 (4)  执行    =》    PS E:\MongoDB\Server\4.0\bin> mongod --dbpath=D:\node\blog\blog-2\db --port=27018

(5)     也可以通过可视化工具 robomongodb 来连接 mongodb

(6)     文档 : https://mongoosejs.com/docs/connections.html

一, 入口文件 app.js

mongoose.connect('mongodb://localhost:27018/blog-2', {useNewUrlParser: true},(err)=>{  // blog-2 为项目目录的名称
  if(err) {
    console.log('数据库连接失败')
  }else{
    console.log('数据库连接成功')
    app.listen(3000)
  }
}) 
 
二, 定义数据库表文件  /schema/users.js  
 
var mongoose = require('mongoose')
/**
*  创建表结构
*/
module.exports = new mongoose.Schema({
  username:String,
  password:String
})
 
三, 定义模型类文件,用于对用户的增删改查  /models/User.js    ( 模型类的使用 :https://mongoosejs.com/docs/models.html )
 
var mongoose = require('mongoose')
userSchema = require('../schemas/users')
/**
* 创建模型
*/
module.exports= mongoose.model('User',userSchema)
 
 
——————————————————————————
 
补充:
 

1、先安装mongoose
npm install mongoose -D

2、启动mongodb
cd 你的mongodb安装目录/bin
dbpath后面跟的路径是你数据库 数据文件存放位置,自己随便存一个地方
命令: mongod --dbpath=D:\node\blog\blog-2\db --port=27018

3、在schemas中定义各个模块的schema数据库表结构,数据库表结构定义好后,导出表结构
const mongoose = require('mongoose')
const Schema = mongoose.Schema

// 用户表的数据结构
const user = new Schema({
  // 用户名
  username: String,
  // 密码
  password: String
})

module.exports = user

4、在models中定义各自的model操作方法,当然需要再modals相应的模块中引入 schema
const mongoose = require('mongoose')
const usersSchema = require('../schemas/user')

// 通过 mongoose创建模型
const UserModel = mongoose.model('User', usersSchema)

// 将这个模型导出 model上是可操作数据的 CURD
module.exports = UserModel

5,记得在app.js中,就是后台 express或者你其他框架或者原生node的入口文件中,连接数据库
const mongoose = require('mongoose')
const db = mongoose.connect('mongodb://localhost:27017/huaxia', (err)=> {
if(err) {
console.log('连接失败')
return
}
console.log('连接成功')
})
 

5 ~ express ~ 连接数据库的更多相关文章

  1. express 连接数据库

    (1)创建项目 ,项目名cntMongodb express -e cntMongodbcd cntMonfodbnpm installnpm install mongoose --save //安装 ...

  2. express连接数据库 读取表

    connection 连接数据库    connection.query 查询表   1.依赖 const mysql = require('mysql'); 连接数据库代码 var connecti ...

  3. mongoDB3.0版本使用express读取数据

    使用express连接数据库操作 var express = require('express'); var app = express(); var MongoClient = require('m ...

  4. 基于 Sequelize.js + Express.js 开发一套 Web 后端服务器

    什么是 Sequelize 我们知道 Web 应用开发中的 Web 后端开发一般都是 Java.Python.ASP.NET 等语言.十年前,Node.js 的出现使得原本仅限于运行在浏览器中的 Ja ...

  5. 前端笔记之NodeJS(四)MongoDB数据库&Mongoose&自制接口&MVC架构思想|实战

    一.MongoDB数据库 1.1 NoSQL简介 随着互联网web2.0网站的兴起,传统的SQL数据库(关系数据库)在应付web2.0网站,特别是超大规模和高并发的SNS(social network ...

  6. 山西WebGIS项目总结

    有一段时间没写blog了,说实话,最近的心态一直在变化,看了一部日剧,回想了这一年所学所见,感觉生活目标变了. 做国土项目这段时间不是很忙,由于数据一直给不到位,时间拖得很久,所以在这期间也在继续学习 ...

  7. Node-Blog整套前后端学习记录

    Node-Blog 后端使用node写的一个一整套的博客系统 #### 主要功能 登录 注册 发表文章 编辑/删除文章 添加/删除/编辑文章分类 账号的管理 评论功能 ... 所用技术 node ex ...

  8. bash: express: command not found及vue连接数据库调接口

    今天在使用express -e . 的命令时,cmd给我报了一段不识别的错误: bash: express: command not found ,在网上查了一下,有人指出是express4的版本将命 ...

  9. node连接数据库(express+mysql)

    操作是在ubuntu系统的下环境,简单记录一下过程. 首先用apt-get安装数据库,键入命令 sudo apt-get install mysql-server , 一路回车,然后在一个界面设置一下 ...

随机推荐

  1. Address localhost:1099 is already in use(IDEA启动Tomcat报错1099 is already in use)

    IDEA中启动Tomcat报错,Error running Tomcat7.0.52: Address localhost:1099 is already in use 或者是 java.rmi.se ...

  2. 「Luogu1901」发射站

    传送门 Luogu 解题思路 单调栈裸题,扫两遍处理出每个点左边第一个比他高的和右边第一个比他高的,然后模拟题意即可. 细节注意事项 咕咕咕. 参考代码 #include <algorithm& ...

  3. css限制文字显示字数长度,超出部分自动用省略号显示,防止溢出到第二行

    为了保证页面的整洁美观,在很多的时候,我们常需要隐藏超出长度的文字.这在列表条目,题目,名称等地方常用到. 效果如下: 未限制显示长度,如果超出了会溢出到第二行里.严重影响用户体验和显示效果. 我们在 ...

  4. leetcode713 Subarray Product Less Than K

    """ Your are given an array of positive integers nums. Count and print the number of ...

  5. leetcode841 Keys and Rooms

    """ There are N rooms and you start in room 0. Each room has a distinct number in 0, ...

  6. greenplum 存储过程 输出信息

    raise notice 'just a simple output msg';

  7. 蓝桥杯 能量项链 (区间dp)

    问题描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定 ...

  8. HTML速写

    1. E 代表HTML标签. 2. E#id 代表id属性. 3. E.class 代表class属性. 4. E[attr=foo] 代表某一个特定属性. 5. E{foo} 代表标签包含的内容是f ...

  9. css滚动

    css 滚动transform: translateY(-100px);jquery $(box).height(); //获取元素高度$(box).scrollTop();//获得元素的滚动条高度

  10. cenos7配置confluence+mysql5.6

    一.准备阶段 我的环境为 腾讯云镜像centos7.4 ,centos 内置 mariadb  需要先删除 #检查是否安装了 mariadb rpm -qa |grep mariadb #删除mari ...