MongoDB数据库基础
MongoDB简介
MongoDB是一种文档型的非关系型数据库(NoSQL),举例如下:
{“foo”:3,"greeting":"Hello,world!"}
并且区分大小写,key值唯一,不可重复,文档可以嵌套,键值对是有序的
集合
1、集合就是一组文档
2、文档类似于关系库里的行
3、集合类似于关系库里的表
4、集合里的文档无需固定的结构(与关系型数据库的区别)
集合的命名
1、不能是空字符串
2、不能包含\0字符(空字符)
3、不能使用system.的前缀
4、建议不包含保留字“$”
5、用.分割不同命名空间的子集合(如:blog.users)
数据库
1、多个文档组成集合,多个集合组成数据库
2、一个实例可以承载多个数据库
3、每个数据库都有独立的权限
4、保留的数据库名称(admin,local,config)
命令行操作MongoDB
参考菜鸟教程
在\MongoDB\Server\3.4\bin进入命令行窗口,然后敲mongo即进入MongoDB服务,显示所有数据库
> show dbs;
使用某一数据库(如果不存在的话则会自动新建)
> use 数据库名称;
插入数据
写数据
> stu = {
name:'Jhon',
age:21,
"sex":"male"
}
插入数据
> db.数据库名称.insert(stu)
查询数据
> db.数据库名称.find()
更新数据
> stu_obj = db.students.findOne()
{
"_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"),
"name" : "Jhon",
"age" : 21,
"sex" : "male"
}
> stu_obj.name = "Jackchen"
Jackchen > db.students.update({name:"Jhon"},stu_obj)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.students.findOne()
{
"_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"),
"name" : "Jackchen",
"age" : 21,
"sex" : "male"
}
更新数据时,update里第二个参数会完全代替前一个参数,所以如果第二个参数只有{name:"**"},会把原来的数据删除
如果要更新多个数据,则可以使用updateMany
总结:这种方式更新数据相当麻烦,可以用修改器
删除数据
原来是三条数据:
> db.students.find()
{ "_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"), "name" : "Jhon", "age" : 21, "sex" : "male" }
{ "_id" : ObjectId("5a9a04d9b1489e70f3a89a0d"), "name" : "oldhu" }
{ "_id" : ObjectId("5a9a09a3b1489e70f3a89a0e"), "name" : "Amy", "age" : 16, "sex" : "male" }
删除其中一条:
> db.students.remove({name:"oldhu"})
WriteResult({ "nRemoved" : 1 })
> db.students.find()
{ "_id" : ObjectId("5a9a01c5b1489e70f3a89a0c"), "name" : "Jackchen", "age" : 21, "sex" : "male" }
{ "_id" : ObjectId("5a9a09a3b1489e70f3a89a0e"), "name" : "Amy", "age" : 16, "sex" : "male" }
删除全部:
> db.students.remove({})
WriteResult({ "nRemoved" : 2 })
> db.students.find()
>
练习
1、创建一个学生信息表(包含:姓名,性别,成绩,年龄)
2、写入十五条不同的数据
3、查询所有的男生数据(只需要学生的姓名和年龄)
4、查询成绩合格的学生信息(学生成绩大于或等于60分)
5、查询所有18岁的男生和16岁的女生的数据
6、按照学生的年龄进行排序
7、将所有的学生年龄增加一岁
步骤1和2、
先在文本编辑器里面编辑好文档,推荐使用sublimeText3或者Notepad++
> use students;
> db.students.insertMany(
... [
... {name:"bob",age:14,sex:"male",grade:95},
... {name:"ahn",age:18,sex:"female",grade:45},
... {name:"xi",age:15,sex:"male",grade:75},
... {name:"hiy",age:23,sex:"male",grade:85},
... {name:"young",age:21,sex:"male",grade:45},
... {name:"nive",age:25,sex:"female",grade:55},
... {name:"longsi",age:19,sex:"male",grade:95},
... {name:"puki",age:16,sex:"female",grade:95},
... {name:"bbc",age:26,sex:"male",grade:95},
... {name:"hippop",age:54,sex:"male",grade:95},
... {name:"sue",age:21,sex:"feale",grade:95},
... {name:"yang",age:36,sex:"male",grade:95}
... ]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5a9a10adb1489e70f3a89a0f"),
ObjectId("5a9a10adb1489e70f3a89a10"),
ObjectId("5a9a10adb1489e70f3a89a11"),
ObjectId("5a9a10adb1489e70f3a89a12"),
ObjectId("5a9a10adb1489e70f3a89a13"),
ObjectId("5a9a10adb1489e70f3a89a14"),
ObjectId("5a9a10adb1489e70f3a89a15"),
ObjectId("5a9a10adb1489e70f3a89a16"),
ObjectId("5a9a10adb1489e70f3a89a17"),
ObjectId("5a9a10adb1489e70f3a89a18"),
ObjectId("5a9a10adb1489e70f3a89a19"),
ObjectId("5a9a10adb1489e70f3a89a1a")
]
}
> db.students.find()
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 }
步骤3、
> db.students.find({sex:'male'},{name:1,age:1,_id:0})
{ "name" : "bob", "age" : 14 }
{ "name" : "xi", "age" : 15 }
{ "name" : "hiy", "age" : 23 }
{ "name" : "young", "age" : 21 }
{ "name" : "longsi", "age" : 19 }
{ "name" : "bbc", "age" : 26 }
{ "name" : "hippop", "age" : 54 }
{ "name" : "yang", "age" : 36 }
步骤4、
> db.students.find({grade:{'$gte':60}})
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 }
步骤5、
> db.students.find({'$or':[{sex:'male',age:18},{sex:'female',age:16}]})
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 }
步骤6、(升序)
> db.students.find().sort({age:1})
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 }
(降序)
> db.students.find().sort({age:-1})
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75 }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95 }
步骤7、(使用修改器来更新)
例子:将所有集合增加一个address项,值是‘---’
> db.students.update({},{'$set':{address:'---'}},{multi:true})
WriteResult({ "nMatched" : 12, "nUpserted" : 0, "nModified" : 12 })
> db.students.find()
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 14, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 18, "sex" : "female", "grade" : 45, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 15, "sex" : "male", "grade" : 75, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 23, "sex" : "male", "grade" : 85, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 21, "sex" : "male", "grade" : 45, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 25, "sex" : "female", "grade" : 55, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 19, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 16, "sex" : "female", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 26, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 54, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 21, "sex" : "feale", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 36, "sex" : "male", "grade" : 95, "address" : "---" }
所有学生年龄增加一岁:
> db.students.update({},{'$inc':{age:1}},{multi:true})
WriteResult({ "nMatched" : 12, "nUpserted" : 0, "nModified" : 12 })
> db.students.find()
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a0f"), "name" : "bob", "age" : 15, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 19, "sex" : "female", "grade" : 45, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a11"), "name" : "xi", "age" : 16, "sex" : "male", "grade" : 75, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a12"), "name" : "hiy", "age" : 24, "sex" : "male", "grade" : 85, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a13"), "name" : "young", "age" : 22, "sex" : "male", "grade" : 45, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 26, "sex" : "female", "grade" : 55, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a15"), "name" : "longsi", "age" : 20, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 17, "sex" : "female", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a17"), "name" : "bbc", "age" : 27, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a18"), "name" : "hippop", "age" : 55, "sex" : "male", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a19"), "name" : "sue", "age" : 22, "sex" : "feale", "grade" : 95, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a1a"), "name" : "yang", "age" : 37, "sex" : "male", "grade" : 95, "address" : "---" }
所有女学生年龄增加一岁:
> db.students.update({sex:"female"},{'$inc':{age:1}},{multi:true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.students.find({sex:"female"})
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a10"), "name" : "ahn", "age" : 20, "sex" : "female", "grade" : 45, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a14"), "name" : "nive", "age" : 27, "sex" : "female", "grade" : 55, "address" : "---" }
{ "_id" : ObjectId("5a9a10adb1489e70f3a89a16"), "name" : "puki", "age" : 18, "sex" : "female", "grade" : 95, "address" : "---" }
MongoDB数据库可视化工具
这里用的是Robo 3T,可以方便查看数据库,以及实现增删改查操作
MongoDB数据库基础的更多相关文章
- MongoDB数据库基础操作
前面的话 为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为Mongodb是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储 ...
- MongoDB数据库 : 基础
三元素:数据库 集合 文档(json的扩展bson) 服务启动重启停止: sudo service mongodb start(stop,restart) 修改配置文件 /etc/mongodb.co ...
- 004.MongoDB数据库基础使用
一 数据库管理 1.1 创建数据库 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin & ...
- MongoDB数据库-基础篇
一使用mongodb 1.常用的命令 show dbs 显示数据库列表 use dbname 进入dbname数据库,大小写敏感,没有这个数据库也不要紧 show collections ...
- 01-MongoDB数据库基础
一.MongoDB数据库基础 1.MongoDB数据库介绍 什么是MongoDB? MongoDB是非关系型数据库中的一种,是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情 ...
- 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用
在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...
- 数据库基础 非关系型数据库 MongoDB 和 redis
数据库基础 非关系型数据库 MongoDB 和 redis 1 NoSQL简介 访问量增加,频繁的读写 直接访问(硬盘)物理级别的数据,会很慢 ,关系型数据库的压力会很大 所以,需要内存级的读写操作, ...
- SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑
本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...
- node.js零基础详细教程(6):mongodb数据库操作
第六章 建议学习时间4小时 课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...
随机推荐
- (转)Unity3D - 动作动画忽略timeScale
转自:http://blog.csdn.net/ynnmnm/article/details/46866347 最近在调战斗时的动画与特效,Unity3D对加/减速提供了Time.timeScale支 ...
- one or more multiply defined symbols found
在一个c++的.h文件中加入了这段代码: #include <string> using namespace std; std::string escapeStr(const std::s ...
- 利用python制作在Linux服务器后台定时运行的任务-邮件提醒
1. 自动任务的功能为: 定时扫描数据库中的记录,然后发邮件 代码如下 scheduleMail.py import pymysql import smtplib from email.mime.te ...
- PPPoE图解
- Abp zero 示例运行
https://aspnetboilerplate.com/Pages/Documents/Zero/Startup-Template-Core Introduction The easiest wa ...
- Flume采集Nginx日志到HDFS
下载apache-flume-1.7.0-bin.tar.gz,用 tar -zxvf 解压,在/etc/profile文件中增加设置: export FLUME_HOME=/opt/apache-f ...
- 研究傅里叶变换的一本好书<<快速傅里叶变换及其C程序>>
快速傅里叶变换及其C程序 <快速傅里叶变换及其C程序>是中国科学技术大学出版社出版的.本书系统地介绍了傅里叶变换的理论和技术,内容包括傅里叶变换(FT)的定义.存在条件及其性质,离散傅里叶 ...
- [转]BLAS简介
BLAS(Basic Linear Algebra Subprograms)是一组线性代数计算中通用的基本运算操作函数集合[1] .BLAS Technical (BLAST) Forum负责规范BL ...
- http头文件User-Agent详解【转载】
原文地址:http://blog.csdn.net/andybbc/article/details/50587359 http头文件User-Agent详解 什么是User-Agent User-Ag ...
- Appium 输入 & 符号,实际输入&-
2018-10-11 12:27:12:178 - [debug] [MJSONWP] Calling AppiumDriver.setValue() with args: [["& ...