MongoDB
一、概念
非关系型数据库,保存数据非常灵活
MongoDB是一个介于关系型数据库和非关系型数据库啊之间的产品,是非关系型数据库当中功能最丰富,最像关系型数据库的。它支持的数据结构非常松散,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库表单查询的绝大部分功能,而且还支持对数据建立索引。(索引 )
二、SQL与NoSQL的区别
SQL:数据库------表------数据
NoSQL:数据库------集合(表)------文档(数据)
Mongo的优势
三、安装
MacOS系统安装mongodb教程链接:安装教程
windows安装教程
MongoDB shell version v4. 4.5
connecting to: mongodb: // 127.0 .0 .1 : 27017 / ?compressors= disabled& gssapiServiceName= mongodb
Implicit session: session {
"id" : UUID( "08893c5f-ca3a-40b5-948f-beadf475331f" ) }
MongoDB server version: 4.4 .5
- - -
The server generated these startup warnings when booting:
2021 - 04 - 16T05: 36 : 24.282 + 08 : 00 : Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
- - -
四、Mongo的基本使用
1、查看数据库
show dbs
MongoDB Enterprise > show dbs
admin 0. 000GB
config 0. 000GB
local 0. 000GB
以上三个数据库都是mongo自带的
2、使用/创建数据库
use admin
MongoDB Enterprise > use admin
switched to db admin
意思是使用admin数据库
use demo
MongoDB Enterprise > use demo
switched to db demo
MongoDB Enterprise > db
demo
MongoDB Enterprise > show dbs
admin 0. 000GB
config 0. 000GB
local 0. 000GB
此时use的功能是创建新的书库据demo
因为新建的数据库只存在内存之中,并没有保存在硬盘当中
新建的这个数据库是一个表或者集合
3、查看当前使用的数据库
db
MongoDB Enterprise > db
admin
查看当前使用的数据库
4、看淡数据库当中的表
第一种
MongoDB Enterprise > show tables
system. version
第二种
MongoDB Enterprise > show collections
system. versio
5、向当前数据库插入数据
非手动添加数据
MongoDB Enterprise > db
demo
MongoDB Enterprise > db. jerry. insert( {
s: 1 } )
WriteResult( {
"nInserted" : 1 } )
MongoDB Enterprise > show dbs
admin 0. 000GB
config 0. 000GB
demo 0. 000GB
local 0. 000GB
手动添加数据
db. creatCollection( name, options)
name: 表示集合(表)的名字- - - - - - 注意表的名字不能重复
options:表示可选参数,可以指定表的大小
MongoDB Enterprise > db. createCollection( 'wangjiaxin_cllection' )
{
"ok" : 1 }
MongoDB Enterprise > db. createCollection( 'wangjiaxin1' , {
capped: true, size: 4 } )
{
"ok" : 1 }
上述的size:4 表示该表最多插入6 条数据
在mongo中如果字节小于256 就默认是256 个字节
6、删除数据库的数据
MongoDB Enterprise > show tables
jerry
MongoDB Enterprise > db. dropDatabase( )
{
"dropped" : "demo" , "ok" : 1 }
MongoDB Enterprise > show dbs
admin 0. 000GB
config 0. 000GB
local 0. 000GB
7、查看表中的数据
查询数据
db. name. find( )
MongoDB Enterprise > db. wangjiaaxin_collection. find( )
MongoDB Enterprise > db. wangjiaxin. find( )
{
"_id" : ObjectId( "60793c2b9dc82dc5d7862223" ) , "x" : 1 }
8、查看表是否存在上限
MongoDB Enterprise > show tables
wangjiaxin
wangjiaxin1
wangjiaxin_cllection
MongoDB Enterprise > db. wangjiaxin. isCapped( )
false
返回false表示集合不存在上限
MongoDB Enterprise > db. wangjiaxin1. isCapped( )
true
返回ture表示集合存在上限
9、删除数据库的表
MongoDB Enterprise > show tables
wangjiaxin
wangjiaxin1
wangjiaxin_cllection
MongoDB Enterprise > db. wangjiaxin_cllection. drop( )
true
MongoDB Enterprise > show tables
wangjiaxin
wangjiaxin1
10插入数据补充
向数据库已经存在的表内插入数据(通过ID插入替换数据)
第一种方式:插入单条数据
db. wangjiaxin. insert( {
name: 'wangjiaxin' , age: 25 , gender: 'madl' , id : 1 } )
WriteResult( {
"nInserted" : 1 } )
> db. wangjiaxin. find( )
{
"_id" : ObjectId( "607a764e4474722e1152124d" ) , "x" : 1 }
{
"_id" : ObjectId( "607a79704474722e1152124e" ) , "name" : "wangjiaxin" , "age" : 25 , "gender" : "male" }
{
"_id" : ObjectId( "607a7a264474722e1152124f" ) , "name" : "wangjiaxin" , "age" : 25 , "gender" : "madl" , "id" : 1 }
> db. wangjiaxin. insert( {
name: 'wangjiaxin' , age: 25 , gender: 'madl' , _id: 1 } )
WriteResult( {
"nInserted" : 1 } )
> db. wangjiaxin. find( )
{
"_id" : ObjectId( "607a764e4474722e1152124d" ) , "x" : 1 }
{
"_id" : ObjectId( "607a79704474722e1152124e" ) , "name" : "wangjiaxin" , "age" : 25 , "gender" : "male" }
{
"_id" : ObjectId( "607a7a264474722e1152124f" ) , "name" : "wangjiaxin" , "age" : 25 , "gender" : "madl" , "id" : 1 }
{
"_id" : 1 , "name" : "wangjiaxin" , "age" : 25 , "gender" : "madl" }
注意:主keyID不能重复_id: 1
第二种方式:插入多条数据
> db. wangjiaxin1. insert( {
name: 'wangjiaxin' , age: 25 , gender: 'male' } )
WriteResult( {
"nInserted" : 1 } )
> db. wangjiaxin1. find( )
{
"_id" : ObjectId( "607a7df54474722e11521251" ) , "name" : "wangjiaxin" , "age" : 25 , "gender" : "male" }
> db. wangjiaxin1. insert( [ {
name: 'wangjiaxin' , age: 25 } , {
name: 'lirui' , age: 23 } ] )
BulkWriteResult( {
"writeErrors" : [ ] ,
"writeConcernErrors" : [ ] ,
"nInserted" : 2 ,
"nUpserted" : 0 ,
"nMatched" : 0 ,
"nModified" : 0 ,
"nRemoved" : 0 ,
"upserted" : [ ]
} )
> db. wangjiaxin1. find( )
{
"_id" : ObjectId( "607a7df54474722e11521251" ) , "name" : "wangjiaxin" , "age" : 25 , "gender" : "male" }
{
"_id" : ObjectId( "607a7e5c4474722e11521252" ) , "name" : "wangjiaxin" , "age" : 25 }
{
"_id" : ObjectId( "607a7e5c4474722e11521253" ) , "name" : "lirui" , "age" : 23 }
>
批量添加数据的方式
for ( i= 2 ; i< 10 ; i+ + ) db. wangjiaxin3. insert( {
x: i} )
WriteResult( {
"nInserted" : 1 } )
> db. wangjiaxin3. find( )
{
"_id" : ObjectId( "607a7f824474722e11521254" ) , "x" : 2 }
{
"_id" : ObjectId( "607a7f824474722e11521255" ) , "x" : 3 }
{
"_id" : ObjectId( "607a7f824474722e11521256" ) , "x" : 4 }
{
"_id" : ObjectId( "607a7f824474722e11521257" ) , "x" : 5 }
{
"_id" : ObjectId( "607a7f824474722e11521258" ) , "x" : 6 }
{
"_id" : ObjectId( "607a7f824474722e11521259" ) , "x" : 7 }
{
"_id" : ObjectId( "607a7f824474722e1152125a" ) , "x" : 8 }
{
"_id" : ObjectId( "607a7f824474722e1152125b" ) , "x" : 9 }
根据主key去做数据更新
> db. wangjiaxin3. find( )
{
"_id" : ObjectId( "607a7f824474722e11521254" ) , "x" : 2 }
{
"_id" : ObjectId( "607a7f824474722e11521255" ) , "x" : 3 }
{
"_id" : ObjectId( "607a7f824474722e11521256" ) , "x" : 4 }
{
"_id" : ObjectId( "607a7f824474722e11521257" ) , "x" : 5 }
> db. wangjiaxin3. save( {
_id: ObjectId( "607a7f824474722e11521254" ) , name: 18 , gender: 'male' } )
WriteResult( {
"nMatched" : 1 , "nUpserted" : 0 , "nModified" : 1 } )
> db. wangjiaxin3. find( )
{
"_id" : ObjectId( "607a7f824474722e11521254" ) , "name" : 18 , "gender" : "male" }
{
"_id" : ObjectId( "607a7f824474722e11521255" ) , "x" : 3 }
{
"_id" : ObjectId( "607a7f824474722e11521256" ) , "x" : 4 }
{
"_id" : ObjectId( "607a7f824474722e11521257" ) , "x" : 5 }
{
"_id" : ObjectId( "607a7f824474722e11521258" ) , "x" : 6 }
{
"_id" : ObjectId( "607a7f824474722e11521259" ) , "x" : 7 }
{
"_id" : ObjectId( "607a7f824474722e1152125a" ) , "x" : 8 }
{
"_id" : ObjectId( "607a7f824474722e1152125b" ) , "x" : 9 }
也有单独的插入功能
> db. wangjiaxin3. save( {
name: 'abc' , gender: 'male' } )
WriteResult( {
"nInserted" : 1 } )
> db. wangjiaxin3. find( )
{
"_id" : ObjectId( "607a7f824474722e11521254" ) , "name" : 18 , "gender" : "male" }
{
"_id" : ObjectId( "607a7f824474722e11521255" ) , "x" : 3 }
{
"_id" : ObjectId( "607a7f824474722e11521256" ) , "x" : 4 }
{
"_id" : ObjectId( "607a7f824474722e11521257" ) , "x" : 5 }
{
"_id" : ObjectId( "607a7f824474722e11521258" ) , "x" : 6 }
{
"_id" : ObjectId( "607a7f824474722e11521259" ) , "x" : 7 }
{
"_id" : ObjectId( "607a7f824474722e1152125a" ) , "x" : 8 }
{
"_id" : ObjectId( "607a7f824474722e1152125b" ) , "x" : 9 }
{
"_id" : ObjectId( "607a81d14474722e1152125c" ) , "name" : "abc" , "gender" : "male" }
11、查询数据补充
查询数据库
show dbs
查询表
show tables/ collection
查询表里面的数据
db. name. find( )
> db. stu. find( {
name: '张三' } )
{
"_id" : ObjectId( "607a96864474722e1152125d" ) , "name" : "张三" , "hometown" : "长沙" , "age" : 20 , "gender" : true }
美化查询(格式化的查询打印)
> db. stu. find( {
name: '老李' } ) . pretty( )
{
"_id" : ObjectId( "607a96864474722e1152125e" ) ,
"name" : "老李" ,
"hometown" : "广州" ,
"age" : 18 ,
"gender" : false
}
查询一条
> db. stu. findOne( )
{
"_id" : ObjectId( "607a96864474722e1152125d" ) ,
"name" : "张三" ,
"hometown" : "长沙" ,
"age" : 20 ,
"gender" : true
}
条件查找
db. stu. find( {
age: 18 } )
{
"_id" : ObjectId( "607a96864474722e1152125e" ) , "name" : "老李" , "hometown" : "广州" , "age" : 18 , "gender" : false }
{
"_id" : ObjectId( "607a96864474722e1152125f" ) , "name" : "王麻子" , "hometown" : "北京" , "age" : 18 , "gender" : false }
{
"_id" : ObjectId( "607a96864474722e11521263" ) , "name" : "老amy" , "hometown" : "衡阳" , "age" : 18 , "gender" : true }
12、比较运算符
> db. stu. find( {
age: {
$gt: 18 } } )
{
"_id" : ObjectId( "607a96864474722e1152125d" ) , "name" : "张三" , "hometown" : "长沙" , "age" : 20 , "gender" : true }
{
"_id" : ObjectId( "607a96864474722e11521260" ) , "name" : "刘六" , "hometown" : "深圳" , "age" : 40 , "gender" : true }
{
"_id" : ObjectId( "607a96864474722e11521262" ) , "name" : "小永" , "hometown" : "广州" , "age" : 45 , "gender" : true }
> db. stu. find( {
age: {
$gte: 18 } } )
{
"_id" : ObjectId( "607a96864474722e1152125d" ) , "name" : "张三" , "hometown" : "长沙" , "age" : 20 , "gender" : true }
{
"_id" : ObjectId( "607a96864474722e1152125e" ) , "name" : "老李" , "hometown" : "广州" , "age" : 18 , "gender" : false }
{
"_id" : ObjectId( "607a96864474722e1152125f" ) , "name" : "王麻子" , "hometown" : "北京" , "age" : 18 , "gender" : false }
{
"_id" : ObjectId( "607a96864474722e11521260" ) , "name" : "刘六" , "hometown" : "深圳" , "age" : 40 , "gender" : true }
{
"_id" : ObjectId( "607a96864474722e11521262" ) , "name" : "小永" , "hometown" : "广州" , "age" : 45 , "gender" : true }
{
"_id" : ObjectId( "607a96864474722e11521263" ) , "name" : "老amy