博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Node+express+mongoose 基础笔记
阅读量:7167 次
发布时间:2019-06-29

本文共 2580 字,大约阅读时间需要 8 分钟。

本篇文章主要介绍mongoose的一些常用api。

安装数据库连接中间件

npm install mongoose -s

进入mongodb安装目录,找到bin文件夹执行命令

> mongod --dbpath=项目的db路径 注:每次重新连接之前,需要把 .lock文件删掉

可以去官网下载mongodb可视化的操作工具,操作数据库

https://robomongo.org/download

首先,我们还是需要搭建node + express架构

// 构建express服务器var express = require('express');var server = express();// 采用Promise,判断,先连接数据库成功后启动服务器。new Promise((resolve,reject)=>{//连接mongodbvar mongoose = require('mongoose');mongoose.connect('mongodb://localhost:27017',(error)=>{    if(error) {        console.log('数据库连接失败');        console.log(error);    }else {        console.log('数据库连接成功');        resolve();    }})}).then(()=>{    server.listen(8080,'localhost',(req,res)=>{        console.log('服务器启动 @ localhost:8080');   })   // 将数据库的模型操作封装到handleDB js文件中,当服务器启动成功之后,获取db model的数据   require('./handleDB');})

新建js文件handleDB

var mongoose = require('mongoose');//定义表字段以及字段类型var userSchema = ({    username:String,    password:String,    age:Number,    sex:{        type:String,        default:'女'    }})
// 表的名字 userconst UserModel = mongoose.model('user',userSchema);

插入一条数据

const userModel = new UserModel({    username:"aaa",    password:'223434',    age:22,    sex:'女'})userModel.save().then((result)=>{    if(result) {        console.log('一条数据插入成功');        console.log(result);    } else {        console.log('数据保存失败');    }});

组装条件查询

//按照条件查询,使用whereUserModel.where({    username:'aaa'}).find().then(res=>{    if(res) {        console.log('--------------findWhere-------------------');        console.log(res);    }})//也可以把条件写到find({})里面,实现where同样的效果UserModel.find({    username:'aaa'}).then(res=>{    if(res) {        console.log('--------------find()-----------------------');        console.log(res);    }})

根据id查询

UserModel.findById("5acc7d3b948dfe204475d02e").then(res=>{    if(res) {        console.log("-------------------findById------------------");        console.log(res);    }})

update操作

/修改操作,修改查询到的第一个UserModel.update(    //条件查询    {age:22},    {sex:'nvnvnv'}).then(res=>{    console.log('---------------------update-----------------')    console.log(res);})UserModel.findByIdAndUpdate('5acc7d3b948dfe204475d02e',{username:'hahaaaaaaaaaaaaaaaaa'}).then(res=>{    console.log('-----------findByIdAndUpdate-----------');    console.log(res);})UserModel.findOneAndUpdate({username:'aaa',username:'dh'}).then(res=>{    if(res) {        console.log('--------------findOneAndUpdate-----------');        console.log(res);    }})

删除操作

UserModel.remove({username:'aaa2'}).then(res=>{    if(res) {        console.log('----------remove0-------------');        console.log(res);    }})

转载地址:http://vxtwm.baihongyu.com/

你可能感兴趣的文章
int *i = new int;
查看>>
CCF计算机认证——字符串匹配问题(运行都正确,为什么提交后只给50分?)...
查看>>
POST提交的四种类型
查看>>
DataTable转List<T>
查看>>
安卓项目开发
查看>>
使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
查看>>
2019-06-11 Java学习日记之Bootstrap
查看>>
解决apache 443端口被占用
查看>>
PHP中PDO错误/异常(PDOException)处理
查看>>
中国科学院大学生创新实践训练计划-
查看>>
洛谷——P1294 高手去散步
查看>>
python正则表达式
查看>>
Blob写入文件
查看>>
Again Stone Game
查看>>
MySQL 5.7.9修改root密码以及新特性
查看>>
HTML5 —— 属性
查看>>
Oracle Alter Table Modify列语句
查看>>
CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-3禁止交换和禁用大页面
查看>>
php测试题整理(0519)
查看>>
winform下重画ListBox
查看>>