1.mongodb官网
MongoDB: the application data platform | MongoDB
2.进入MongoDB官网下载MongoDB以及MongoDB compass 和Mongodb--database--tools
3.nodejs操作MongoDB数据库需要依赖nodejs的第三方包mongoose?
终端指令: npm install mongoose
4.

?5.
以管理员身份运行PowerShell cd到文件所在目录 如果没有开启MongoDB的话?
使用net start mongodb 指令启动


?6.
- //引入mongoose模块
- const mongoose = require('mongoose');
- // console.log(mongoose);
-
- //todo 连接数据库
- mongoose.connect('mongodb://localhost/test001')
- .then(() => console.log('数据库链接成功'))
- .catch(erro => console.log('连接失败'))
7.在vscode的集成终端中cd到文件所在目录,使用nodemon 'node 02.js'指令打开文件

?8. 设定集合规则 创建集合并应用规则
- //todo 设定集合规则
- const courseSchema = new mongoose.Schema({
- name: String,
- author: String,
- isPublished: Boolean
- });
- // todo 创建集合并应用规则
- // todo 1.集合名称'' 2.集合规则
- const Course = mongoose.model('Course', courseSchema);
??9. 创建集合实例document的两种方式
- // todo 第一种方式 创建集合实例 文档document
- const course = new Course({
- name:'xiaoguo',
- author:'aaa',
- tags:['node','backend'],
- isPublished:false
- })
- // 将数据保存在数据库中
- course.save();
-
-
-
- //todo 第二种方式 创建文档 不需要使用course.save()方式保存,会自动保存进数据库
- Course.create({
- name:'xiaowei',
- author:'sh',
- isPublished:true
- },(erro,data)=>{
- console.log(erro);
- console.log(data)
- });
- //也支持promise对象
- Course.create({
- name:'xiaoli',
- author:'zz',
- isPublished:true
- }).then(data=> console.log(data))
- .catch(erro=>console.log(erro))
?10. 查询用户集合中的所有文档,返回的是一个数组
- // todo 查询用户集合中的所有文档 返回的是一个数组
- Course.find()
- .then(result =>{console.log(result)})
?11. 通过ID字段查询用户集合中的某个文档,返回数组
- // todo 通过ID字段查询用户集合中的某个文档 返回数组
- Course.find({
- _id:"619b0f75dc5e07d1b9924ee9"
- })
- .then(result =>{console.log(result)})
?12. 根据条件查找文档 如果不写条件返回数据库中的第一条文档 返回一个对象
- // todo 根据条件查找文档 如果不写条件返回数据库中的第一条文档 返回一个对象
- Course.findOne({
- name:'xiaowei'
- })
- .then(result=>console.log(result))
13. 根据范围条件查找文档 $gt 最小值 $lt最大值
- // todo 根据范围条件查找文档
- Course.find({
- age: { $gt: 20, $lt: 50 }
- })
- .then(result => console.log(result))
?14. 查询包含
- // todo 根据范围条件查找文档
- Course.find({
- name: { $in: ['xiao'] }
- })
- .then(result => console.log(result))
?15. 选择要查询的字段并排序 默认升序 降序加个-
- // todo 选择要查询的字段 (升序)
- Course.find().select('name age')
- //相反的顺序用.sort('-age') (降序)
- .then(result => console.log(result))
??16. ?skip跳过前两条数据 limit限制查询数量
- // todo skip跳过前两条数据 limit限制查询数量
- Course.find().skip(2).limit(2)
- .then(result => console.log(result))
? ?17. ?查找一个文档并删除文档 返回值是删除的文档 如果匹配到多个文档 只删除第一个
- // todo 查找一个文档并删除文档 返回值是删除的文档 如果匹配到多个文档 只删除第一个
- Course.findOneAndDelete({
- _id:"619b0f75dc5e07d1b9924ee9"
- })
- .then(result=>console.log(result))
?18. ?删除多个文档 ?返回一个对象 {n:删除的文档数量 ok:1(删除成功)}
- // todo 删除多个文档 返回一个对象 {n:删除的文档数量 ok:1(删除成功)}
- Course.deleteMany({
- _id:"619b0f75dc5e07d1b9924ee9"
- })
- .then(result=>console.log(result))
??19. ?更新单个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值
- // todo 更新单个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值
- Course.updateOne(
- {name:'xiaoguo'},
- {name:'xiaoguoguo'}
- )
- .then(result=>console.log(result))
? ?20. ?更新多个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值
- // todo 更新多个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值
- Course.updateMany(
- {},
- {age:18}
- )
- .then(result=>console.log(result))
? ??21.?设置mongoose验证
?针对String类型字段? ? ??required: [true,'错误说明']? ?必传字段??

??针对String类型字段? ? ??minlength: [n,'错误说明']? ? ?? 最小字段长度
?针对String类型字段? ? ???maxlength: [n,'错误说明']? ? ? 最大字段长度

??针对String类型字段? ? ??trim:true? ? ? ?//去除字符串两头的空格
??针对Number类型字段? ? ??min: [n,'错误说明']? ? ?? 最小数值
?针对Number类型字段? ? ???max: [n,'错误说明']? ? ? 最大数值
?设置时间默认值 当用户未传此字段的数据时 启用当前时间为默认值?

?列举出当前字段可以取的值,必须在范围内上传

?自定义错误信息时的格式

制定规则验证用户传入的值的属性是否符合规范 自定义错误信息 message?

?控制台获取错误信息
?
到此这篇关于MongoDB连接数据库并创建数据等使用方法的文章就介绍到这了,更多相关MongoDB连接数据库内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!