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数据库基础的更多相关文章

  1. MongoDB数据库基础操作

    前面的话 为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为Mongodb是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储 ...

  2. MongoDB数据库 : 基础

    三元素:数据库 集合 文档(json的扩展bson) 服务启动重启停止: sudo service mongodb start(stop,restart) 修改配置文件 /etc/mongodb.co ...

  3. 004.MongoDB数据库基础使用

    一 数据库管理 1.1 创建数据库 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin & ...

  4. MongoDB数据库-基础篇

    一使用mongodb 1.常用的命令 show dbs    显示数据库列表 use dbname    进入dbname数据库,大小写敏感,没有这个数据库也不要紧 show collections ...

  5. 01-MongoDB数据库基础

    一.MongoDB数据库基础 1.MongoDB数据库介绍 什么是MongoDB? MongoDB是非关系型数据库中的一种,是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情 ...

  6. 基于C#的MongoDB数据库开发应用(1)--MongoDB数据库的基础知识和使用

    在花了不少时间研究学习了MongoDB数据库的相关知识,以及利用C#对MongoDB数据库的封装.测试应用后,决定花一些时间来总结一下最近的研究心得,把这个数据库的应用单独作为一个系列来介绍,希望从各 ...

  7. 数据库基础 非关系型数据库 MongoDB 和 redis

    数据库基础 非关系型数据库 MongoDB 和 redis 1 NoSQL简介 访问量增加,频繁的读写 直接访问(硬盘)物理级别的数据,会很慢 ,关系型数据库的压力会很大 所以,需要内存级的读写操作, ...

  8. SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑

    本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...

  9. node.js零基础详细教程(6):mongodb数据库操作

    第六章 建议学习时间4小时  课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑. ...

随机推荐

  1. Java access to the Domino Objects, Part 1

    From: https://www.ibm.com/developerworks/lotus/library/ls-Java_access_pt1/index.html Overview Java a ...

  2. Spark机器学习(8):LDA主题模型算法

    1. LDA基础知识 LDA(Latent Dirichlet Allocation)是一种主题模型.LDA一个三层贝叶斯概率模型,包含词.主题和文档三层结构. LDA是一个生成模型,可以用来生成一篇 ...

  3. libmongoc关于\$pullAll和\$addToSet的一个使用问题记录

    问题描述及测试结果 在使用mongodb时,对一个document中的数组成员进行更新的时候,可以使用$pull $push $pop $addToSet $pullAll和$each $positi ...

  4. (原)MobileNetV2

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9410574.html 论文: MobileNetV2: Inverted Residuals and ...

  5. Uber使用Swift重写APP的踩坑经历及解决方案(转载)

    本文出自Uber移动架构和框架组负责人托马斯·阿特曼于2016年在湾区Swift峰会上的演讲,分享了使用Swfit重写Uber的好与坏.以下为译文: 我是托马斯·阿特曼,目前是Uber移动架构和框架组 ...

  6. Dive into Python

    写这篇文章的原因完全是为了督促自己每天晚上看完两章<Dive Into Python>这本书,因此,很多内容都是摘抄自原书的翻译版或者是自己瞎想,于是就顺带着记录了下来.此前已经看完前两章 ...

  7. MDK-ARM输出HEX文件重命名设置

    输出的可执行文件和库的名称就是在这里定义.比如我们常见输出Hex文件,其名称就是这里定义的.

  8. [Big Data - Kafka] kafka学习笔记:知识点整理

    一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险. ...

  9. 【Android】开源项目UniversalImageLoader及开源框架ImageLoader

    UniversalImageLoader 简单来说就是用于加载图片的一个开源项目,在其项目介绍中是这么写的 支持多线程图片加载 提供丰富的细节配置,比如线程池大小,HTPP请求项,内存和磁盘缓存,图片 ...

  10. 基于jQuery虾米音乐播放器样式代码

    分享一款基于jQuery虾米音乐播放器样式代码.这是一款基于jquery+html5实现的虾米音乐播放器源码下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div c ...