好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MongoDB连接数据库并创建数据等使用方法

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

2

3

4

5

6

7

8

//引入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. 设定集合规则 创建集合并应用规则

?

1

2

3

4

5

6

7

8

9

//todo 设定集合规则

const courseschema = new mongoose. schema ({

     name : string,

     author: string,

     ispublished: boolean

});

// todo 创建集合并应用规则

// todo 1.集合名称 '' 2.集合规则

const course = mongoose.model( 'course' , courseschema);

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

// 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. 查询用户集合中的所有文档,返回的是一个数组

?

1

2

3

// todo 查询用户集合中的所有文档 返回的是一个数组

course.find()

. then (result =>{console.log(result)})

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

?

1

2

3

4

5

// todo 通过id字段查询用户集合中的某个文档 返回数组

course.find({

_id: "619b0f75dc5e07d1b9924ee9"

})

. then (result =>{console.log(result)})

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

?

1

2

3

4

5

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

course.findone({

     name : 'xiaowei'

})

. then (result=>console.log(result))

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

?

1

2

3

4

5

// todo 根据范围条件查找文档

course.find({

     age: { $gt: 20, $lt: 50 }

})

. then (result => console.log(result))

  14. 查询包含

?

1

2

3

4

5

// todo 根据范围条件查找文档

course.find({

     name : { $ in : [ 'xiao' ] }

})

. then (result => console.log(result))

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

?

1

2

3

4

// todo 选择要查询的字段 (升序)

course.find(). select ( 'name age' )

//相反的顺序用.sort( '-age' ) (降序)

. then (result => console.log(result))

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

?

1

2

3

// todo skip跳过前两条数据 limit限制查询数量

course.find().skip(2).limit(2)

. then (result => console.log(result))

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

?

1

2

3

4

5

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

course.findoneanddelete({

    _id: "619b0f75dc5e07d1b9924ee9"

})

. then (result=>console.log(result))

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

?

1

2

3

4

5

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

course.deletemany({

    _id: "619b0f75dc5e07d1b9924ee9"

})

. then (result=>console.log(result))

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

?

1

2

3

4

5

6

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

course.updateone(

{ name : 'xiaoguo' },

{ name : 'xiaoguoguo' }

)

. then (result=>console.log(result))

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

?

1

2

3

4

5

6

// 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连接数据库内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Wildness_/article/details/121478126

查看更多关于MongoDB连接数据库并创建数据等使用方法的详细内容...

  阅读:17次