右键网站 选择添加ftp站点,工商网站如何做实名,长沙招聘网58同城招聘发布,网站制作jian she本项目旨在学习如何快速使用 nodejs 开发后端api#xff0c;并为以后开展其他项目的开启提供简易的后端模版。#xff08;非后端工程师#xff09; 由于文档是代码写完之后#xff0c;为了记录项目中需要注意的技术点#xff0c;因此文档的叙述方式并非开发顺序#xff0…本项目旨在学习如何快速使用 nodejs 开发后端api并为以后开展其他项目的开启提供简易的后端模版。非后端工程师 由于文档是代码写完之后为了记录项目中需要注意的技术点因此文档的叙述方式并非开发顺序并非循序渐进的教学文档。建议配合项目源码node-mongodb-template 。
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (一)项目简介及安装依赖
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (二)项目文件夹架构及路由的设置
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (三)Cors的设置及.env文件的设置
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (四)状态码的使用
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (五)POST上传文件的设置
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (六)token的设置
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (七)MongoDB的设置
【NodeJS】NodeJSmongoDB在线版开发简单RestfulAPI (八)API说明(暂时完结后续考虑将在线版mongoDB变为本地版)
MongoDB的设置
选择了MongoDB的在线版数据库可以申请免费的空间使用点击网址申请。申请和创建库之后复制针对nodejs提供的code如下
mongodbsrv://db:db_passwordtest.gx6wc.mongodb.net/?retryWritestruewmajorityappNametest将其中db_password替换成你设置的密码。
接下来就可以在nodejs中使用该云数据库了。
连接数据库
安装依赖
pnpm i --save mongoose
引用依赖
//app.js
const mongoose require(mongoose);连接数据库
mongoose.connect(mongodbsrv://db:
process.env.MONGO_ATLAS_PW
test.gx6wc.mongodb.net/?retryWritestruewmajorityappNametest);mongoose.Promise global.Promise;mongoose API学习
Model数据结构声明
Type数据类型
mongoose.Schema.Types.ObjectId 自动生成的id;Number 数字型String 字符串型
required是否必要
default默认值
ref关联的表/数据结构/model
unique是否唯一
match数据正则检查匹配
定义数据结构
mongoose.Schema({...})
mongoose.model(modelName, ModelSchema)
//product.js
const mongoose require(mongoose);const productSchema mongoose.Schema({_id:mongoose.Schema.Types.ObjectId,name:{type:String, required:true},price:{type:Number, required:true},productImage:{type:String, required:true},
})module.exports mongoose.model(Product, productSchema);//order.js
const mongoose require(mongoose);const orderSchema mongoose.Schema({_id:mongoose.Schema.Types.ObjectId,//product的值为ObjectId与【Product】关联product:{type:mongoose.Schema.Types.ObjectId, required:true,ref:Product},quantity:{type:Number, default:1},
})module.exports mongoose.model(Order, orderSchema);//user.js
const mongoose require(mongoose);const userSchema mongoose.Schema({_id:mongoose.Schema.Types.ObjectId,email:{type:String, required:true,unique:true,//校验email的格式match:/(?:[a-z0-9!#$%*/?^_{|}~-](?:\.[a-z0-9!#$%*/?^_{|}~-])*|(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*)(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f]))\])/},password:{type:String, required:true},
})module.exports mongoose.model(User, userSchema);数据操作
类型引用
//product Model定义的位置
const Product require(../models/product);exec: 执行
执行mongoose的query操作后调用exec()方法
Product.find().then();
Product.find().exec().then();区别在于 mongoose 的所有查询操作返回的结果都是 query 官方文档是这样写的并非一个完整的promise。 而加上.exec()则将会返回成为一个完整的 promise 对象但是其是 mongoose 自行封装的 promise 与 ES6 标准的 promise 是有所出入的你应该会在控制台看到相关的警告而且官方也明确指出在未来的版本将会废除自行封装的promise改为 ES6 标准因此建议楼主在使用过程中替换为 ES6 的 promise如下 const mongoose require(‘mongoose’); mongoose.Promise global.Promise; find: 查询
//查询全部
Product.find();
Product.find().exec().then(docs{//docs是products数组
});//查找所有满足条件的数据
//条件格式 {prop:value,...}
User.find({email: req.body.email}).exec().then(docs{//docs是数组
})//查询满足条件的数据中的第一个
Product.findOne();
User.findOne({email: req.body.email}).exec().then(doc{//doc是一个对象
})//查询指定Id的数据id唯一
Product.findById();
Product.findById(id).exec().then(doc{//doc是一个对象
})Product.findOneAndDelete();
Product.findOneAndReplace();
Product.findOneAndUpdate()where: 查询条件
Product.where({email: req.body.email}).fineOne().exec().then(doc{});select: 指定属性
//Product包含属性name, price, _id//需求是只显示name和price
Product.find().select(name price).exec().then(docs{//docs是products数组
});populate: 子表属性
//Order包含属性product quantity _id
//product是子表数据的_id//需求是显示product的详细属性
Order.find().select(product quantity).populate(product,name price).exec().then(docs{});
//{
// product:{
// name:,
// price:11
// },
// quantity:1,
//}save: 新增
const product new Product({_id:new mongoose.Types.ObjectId(),name:req.body.name,price:req.body.price,productImage:req.file.path});
product.save().then(result{//result和product实例相同})updateOne: 修改
const updateOpts {name:,price:22,
};
//找到指定id的数据并修改对应字段
Product.updateOne({_id:id},{$set:updateOpts}).exec().then(result{
})deleteOne: 删除
Product.deleteOne({_id:id}).exec().then(result{
})