前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

MongoDB

作者头像
生南星
发布2019-07-22 14:32:40
2.6K0
发布2019-07-22 14:32:40
举报
文章被收录于专栏:生南星生南星

mysql数据库 -- 关系型数据库--表,字段,行 mongodb数据库 -- 非关系型数据库--集合,域,文档 mongodb的使用步骤: 1.安装 2.指定数据库的安装位置(创建一个文件夹,管理mongodb的所有数据) 3.使用mongodb链接该文件夹,Windows下mongodb的命令需要在其安装目录下执行才有效. mongod --dbpath=文件夹路径 4.启动mongodb, mongo 连接mongodb数据库的命令: 1.进入MongoDB下的bin文件夹下--cd 路径 2.命令--mongod --dbpath=C:\Users\lx\Desktop\Node\MongoDB\DB

3.再打开一个cmd 4.重复第一步--cd 路径 5.命令--mongo const mongoose = require('mongoose'); let db=mongoose.connect('mongodb://localhost/tenDB', {useNewUrlParser: true}); mongoose.connection.on("open",function () { console.log("数据库连接成功!"); //创建集合 /* * 语法:new mongoose.Schema() * 参数一:对象, 设置该集合里所需要的域 * 参数二:对象, 指定集合的名字,如果没有,系统会自动创建一个 * */ let schema=new mongoose.Schema({ name:{type:"String",default:"无名"}, age:{type:"Number",default:0}, sex:{type:"String",default:"未知"}, },{ collection:"person" }); //利用集合生成模板,接下来,所有的数据库的操作,如增删查改全部由该模型负责 let model=mongoose.model("person",schema); //增加一条数据 model.create({ name:"白宇", age:29, sex:"男" },function (err,res) { if(!err){ console.log("插入数据成功!"); }else{ console.log("插入数据失败!"); } }); //修改数据 /* * 语法: * model.update({查询条件},{$set:{修改的值}},{multi:true/false}(可选的),function(){}); * */ /*model.update({sex:"男"},{$set:{age:29}},{multi:true},function (err,res) { if(!err){ console.log("修改数据成功",res); }else{ console.log("修改数据失败",err); } });*/ //查找数据 /* * 语法: * model.find({查询条件}可选的,{想要显示的字段}可选的,{筛选条件}可选的,function(){}); * */ /*model.find(function (err,res) { if(!err){ console.log(res); }else{ console.log("查询失败"); } });*/ //给想要显示的字段赋值为1,不想显示的不写即可,但是id如果不想显示,需要给0 /*model.find({sex:"男"},{name:1,age:1,_id:0},function (err,res) { console.log(res); });*/ //查询条件的特殊写法: $gt 大于,$lt 小于,$gte 大于等于,$lte 小于等于,$ne 不等于 /*model.find({age:{$lte:26}},{name:1,sex:1,age:1,_id:0},function (err,res) { console.log(res); });*/ //筛选: sort 排序--1:升序,-1--降序;skip 跳过;limit 限制条数. 如果有排序,先排序,再做其他的跳过操作 /*model.find({},{name:1,sex:1,age:1,_id:0},{limit:4,skip:1,sort:{age:-1}},function (err,res) { console.log(res); });*/ //删除数据 model.remove({删除条件}); /*model.remove({_id:"5caaa31869e6292e24c77c65"},function (err,res) { if(!err){ console.log("删除数据成功",res); }else{ console.log("删除数据失败",err); } });*/ });

总结:

数据持久化: 数据永久的保存起来 1.文件 2.cookie 3.数据库 根据处理数据的能力, 可分为: 1.大型数据库: Oracle 2.中型数据库: MySQL, SQLServer 3.小型数据库: Access 4.轻量级数据库: SQLite 数据库的组成 1.一个数据库系统管理着多个数据库 2.一个数据库中可以存放多张表 3.每张表都有字段(比如姓名, 年龄) 4.表中会有一个特殊的字段(主键), 用于保证数据的唯一性 MySQL的管理系统: phpMyAdmin 通过代码操作数据库, 使用SQL(structure query language, 结构化查询语言) CURD 1.增(insert) 2.删(delete) 3.改(update) 4.查(select) 注: SQL语句中的关键词, 不区分大小写 一.查询语句 1.查询所有数据 select * from 表名 例如: select * from student/SELECT * FROM student 2.查询所有数据, 只显示某些字段 select 字段1, 字段2, ..., 字段n from 表名 例如: select name, gender from student 3.根据某个条件进行查找 select * from 表名 where 字段 = 值 例如: select * from student where gender = '女' 4.根据多个条件进行查找 select * from 表名 where 字段1 = 值1 and 字段2 = 值2 例如: select * from student where name = ‘you’ and age = 2 5.根据范围进行查找 select * from 表名 where 字段 > 值 例如: select * from student where age >= 18 select * from 表名 where 字段 between 值1 and 值2 例如: select * from student where age between 24 and 25 6.反向查找 select * from 表名 where 字段 not between 值1 and 值2 例如: select * from student where age not between 24 and 25 7.根据多个条件中的某个条件, 进行查找 select * from student where 字段1 = 值1 or 字段2 = 值2 例如: select * from student where name = ‘hou’ or age = 18 8.模糊查询, 以什么开头 select * from student where 字段 like 值% 例如: select * from student where name like '张%' 9.模糊查询, 以什么结尾 select * from student where 字段 like %值 例如: select * from student where name like '%张' 10.模糊查询, 包含某个内容 select * from student where 字段 like %值% 例如: select * from student where name like '%张%' 11.不重复查找 select distinct 字段 from 表名 例如: select distinct gender from student 12.限制查询的条数 select * from 表名 limit 条数 例如: select * from student limit 2 13.对查询的结果进行排序 升序: select * from 表名 order by 字段 asc 降序: select * from 表名 order by 字段 desc 例如: select * from student order by age asc 二: 插入语句 insert into 表名 (字段1, 字段2, ..., 字段n) values (值1, 值2, ..., 值3) 例如: insert into student (name, gender, age) values (‘keke’, '女', 38) 三: 修改语句 update 表名 set 字段1 = 值1, ..., 字段n = 值n where 主键 = 值 例如: update student set name = '经纪人' where id = 6 四.删除语句 delete from 表名 where 主键 = 值 例如: delete from student where id = 6 五.新建表 create table 表名(字段1 类型1, ..., 字段n 类型n) 例如: create table if not exists cat(id int primary key auto_increment, nickname text) 六.删除表 drop table 表名 例如: drop table cat

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生南星 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档