mongoDB
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongoDB,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3562字,纯文字阅读大概需要6分钟。
内容图文
![mongoDB](/upload/InfoBanner/zyjiaocheng/872/140d8c421ce049779bac0a064ef09297.jpg)
一、mongoDB + Node.js 的增删改查
mongoDB下载地址 : https://www.mongodb.com/try/download/community
1、node.js依赖于第三方包 :mongoose操作mongoDB, 所以需要先下载 :npm install mongoose
2、需要先引入mongoose 第三方包 : const mongoose = require( ' mongoose ' ) ;
3、连接数据库:
注:node的所有操作数据库的方法,都返回Promise对象,如果连接的数据库不存在,会在添加完数据后自动创建
1 mongoose.connect('mongodb://localhost/ 数据库名 ' ,
{ useNewUrlParser : true, useUnifiedTopology: true }) 2 .then(()=> console.log(' 数据库连接成功 ')) 3 .catch(()=> console.log(' 数据库连接失败 '))
4、创建集合规则(集合相当于数据表), Schema是一个构造函数,在它的实例对象里面填写规则
例如创建一个用户表的规则:
const userSchema = new mongoose.Schema({ name : String, age : Number, email : String, password : String, hobbies : [String] // hobbies 里面存的是一个数组 })
5、创建集合,参数1是集合名(相当于表名),首字母必须大写,但是在数据库中显示为 users,参数2是规则, 返回的是一个构造函数
1 const User = mongoose.model(' User ', userSchema);
6、向集合中添加文档(文档就是数据),用集合返回的构造函数的实例对象添加, 默认会添加id 为 _id
1 const user = new User({ 2 name: '丫丫' , 3 age : 37 , 4 hobbise : ['唱歌','跳舞','好看'], 5 email : "tongliya@love.com" , 6 password: "yayaya" 7 })
7、添加的另一种写法
1 User.create({ 2 name: '丫丫' , 3 age : 37 , 4 hobbise : ['唱歌','跳舞','好看'], 5 email : "tongliya@love.com" , 6 password: "yayaya" 7 }) 8 .then(result=>console.log(result)) 9 .catch(err=>console.log(err))
8、查询
(1)、查询用户集合中的所有文档,返回的是查询到的数据
1 User.find().then(result=>console.log(result))
(2)、根据条件查询数据
1 User.find({age: '20'}).then(result=>console.log(result))
(3)、条件查询,只查询一条数据
1 User.findOne({age: '20'}).then(result=>console.log(result))
(4)、查询年龄大一20,小于50的数据
1 User.find({age: {$gt: '20', $lt: '50'}}).then(result=>console.log(result))
(5)、查询爱好为打豆豆的数据,注:爱好是一个数组
1 User.find({hobbise: {$in: [' 打豆豆 ']}}).then(result=>console.log(result))
(6)、查询某些指定字段, 查询哪些字段就写进select中,用空格隔开,不想查询哪些字段,就在前面加 -, 如 -_id,默认会查询 _id
1 User.find().select(' name email -_id ').then(result=>console.log(result))
(7)、对查询字段进行升序,用sort(),降序只要在字段前加上 -
1 User.find().sort(' age ').then(result=>console.log(result)) // 对查询到的字段按年龄进行升序排列 2 3 User.find().sort(' -age ').then(result=>console.log(result)) // 对查询到的字段按年龄进行降序排列
(8)、查询年龄是10,20,25的选项
1 User.find({age: {$in: [10, 20, 25]}}).then(result=>console.log(result))
(9)、skip 跳过多少条数据, limit 限制查询多少条数据
1 User.find().skip(2).limit(2).then(result=>console.log(then))
// 查询到的数据跳过前两条,剩下的结果中只查询头两条
9、删除
(1)、查找到一条文档并且删除,返回删除的文档,如果查询匹配到多个文档,那么将删除第一条文档
1 User.findOneAndDelete({_id: '5c09f2d9aeb04b22f846096b'}).then(result=>console.log(result));
(2)、删除多条文档,返回值是一个对象 { n: (删除多少条文档), ok: 1, deletedCount: (删除的总数) }
1 User.deleteMany({_id: '5c09f267aeb04b22f8460968'}).then(result=>console.log(result));
10、更新
(1)、更新集合中的文档(更新一个), 第一个参数是查询条件, 第二个参数是修改的值, 返回: { n: 1, nModified: 1, ok: 1 }
1 User.updateOne({name: '李四'}, {age: '30'}).then(result=>console.log(result));
(2)、更新集合中的文档(更新多个), 第一个参数不写表示选择全部
1 User.updateMany({}, {age: '50'}).then(result=>console.log(result));
内容总结
以上是互联网集市为您收集整理的mongoDB全部内容,希望文章能够帮你解决mongoDB所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。