经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MongoDB » 查看文章
MongoDB连接数据库并创建数据等使用方法
来源:jb51  时间:2021/11/24 17:09:55  对本文有异议

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.

  1. //引入mongoose模块
  2. const mongoose = require('mongoose');
  3. // console.log(mongoose);
  4. //todo 连接数据库
  5. mongoose.connect('mongodb://localhost/test001')
  6. .then(() => console.log('数据库链接成功'))
  7. .catch(erro => console.log('连接失败'))

7.在vscode的集成终端中cd到文件所在目录,使用nodemon 'node 02.js'指令打开文件

?8. 设定集合规则 创建集合并应用规则

  1. //todo 设定集合规则
  2. const courseSchema = new mongoose.Schema({
  3. name: String,
  4. author: String,
  5. isPublished: Boolean
  6. });
  7. // todo 创建集合并应用规则
  8. // todo 1.集合名称'' 2.集合规则
  9. const Course = mongoose.model('Course', courseSchema);

??9. 创建集合实例document的两种方式

  1. // todo 第一种方式 创建集合实例 文档document
  2. const course = new Course({
  3. name:'xiaoguo',
  4. author:'aaa',
  5. tags:['node','backend'],
  6. isPublished:false
  7. })
  8. // 将数据保存在数据库中
  9. course.save();
  10. //todo 第二种方式 创建文档 不需要使用course.save()方式保存,会自动保存进数据库
  11. Course.create({
  12. name:'xiaowei',
  13. author:'sh',
  14. isPublished:true
  15. },(erro,data)=>{
  16. console.log(erro);
  17. console.log(data)
  18. });
  19. //也支持promise对象
  20. Course.create({
  21. name:'xiaoli',
  22. author:'zz',
  23. isPublished:true
  24. }).then(data=> console.log(data))
  25. .catch(erro=>console.log(erro))

?10. 查询用户集合中的所有文档,返回的是一个数组

  1. // todo 查询用户集合中的所有文档 返回的是一个数组
  2. Course.find()
  3. .then(result =>{console.log(result)})

?11. 通过ID字段查询用户集合中的某个文档,返回数组

  1. // todo 通过ID字段查询用户集合中的某个文档 返回数组
  2. Course.find({
  3. _id:"619b0f75dc5e07d1b9924ee9"
  4. })
  5. .then(result =>{console.log(result)})

?12. 根据条件查找文档 如果不写条件返回数据库中的第一条文档 返回一个对象

  1. // todo 根据条件查找文档 如果不写条件返回数据库中的第一条文档 返回一个对象
  2. Course.findOne({
  3. name:'xiaowei'
  4. })
  5. .then(result=>console.log(result))

13. 根据范围条件查找文档 $gt 最小值 $lt最大值

  1. // todo 根据范围条件查找文档
  2. Course.find({
  3. age: { $gt: 20, $lt: 50 }
  4. })
  5. .then(result => console.log(result))

?14. 查询包含

  1. // todo 根据范围条件查找文档
  2. Course.find({
  3. name: { $in: ['xiao'] }
  4. })
  5. .then(result => console.log(result))

?15. 选择要查询的字段并排序 默认升序 降序加个-

  1. // todo 选择要查询的字段 (升序)
  2. Course.find().select('name age')
  3. //相反的顺序用.sort('-age') (降序)
  4. .then(result => console.log(result))

??16. ?skip跳过前两条数据 limit限制查询数量

  1. // todo skip跳过前两条数据 limit限制查询数量
  2. Course.find().skip(2).limit(2)
  3. .then(result => console.log(result))

? ?17. ?查找一个文档并删除文档 返回值是删除的文档 如果匹配到多个文档 只删除第一个

  1. // todo 查找一个文档并删除文档 返回值是删除的文档 如果匹配到多个文档 只删除第一个
  2. Course.findOneAndDelete({
  3. _id:"619b0f75dc5e07d1b9924ee9"
  4. })
  5. .then(result=>console.log(result))

?18. ?删除多个文档 ?返回一个对象 {n:删除的文档数量 ok:1(删除成功)}

  1. // todo 删除多个文档 返回一个对象 {n:删除的文档数量 ok:1(删除成功)}
  2. Course.deleteMany({
  3. _id:"619b0f75dc5e07d1b9924ee9"
  4. })
  5. .then(result=>console.log(result))

??19. ?更新单个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值

  1. // todo 更新单个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值
  2. Course.updateOne(
  3. {name:'xiaoguo'},
  4. {name:'xiaoguoguo'}
  5. )
  6. .then(result=>console.log(result))

? ?20. ?更新多个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值

  1. // todo 更新多个文档 里面传两个对象 ,隔开 第一个对象是查询条件 第二个要改的值
  2. Course.updateMany(
  3. {},
  4. {age:18}
  5. )
  6. .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!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号