MongoDB使用手册
theme: csdn
highlight: MongoDB使用手册
简介:
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写,旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系型数据库和非关系型数据库之间的产品,是非关系型数据库当中功能最丰富,最像关系型数据库的。
一. 安装
官网点击下载
网盘点击下载
1. 下载MongoDB安装包,选择windows版;
2. 运行MongoDB安装包并选择自定义安装,设置好安装路径(可以选择自定义安装路径);
3. 配置MongoDB,让MongoDB作为服务运行,配置好数据目录和日志目录;
4. 最终可取消MongoDB Compass的安装选项(不取消安装极慢),需要可自行安装;
5. 双击mongo.exe 即可以运行MongoDB自带客户端,并使用MongoDB:
6. 连接成功后效果如下:
7. 如果需要移除MongoDB服务, 使用管理员权限运行cmd工具, 并输入如下命令结束进程:
> sc.exe delete MongoDB
8. 为了方便可通过网盘下载视图化的客户端工具进行使用。
二. MongoDB的使用
介绍:MongoDB是非关系型数据库当中最像关系型数据库的,所以我们通过它与关系型数据库的对比。
SQL数据库 |
MongoDB数据库 |
概念 |
database |
database |
数据库 |
table |
collection |
数据表(集合List) |
row |
document |
数据记录行(文档) |
column |
field |
数据字段(域) |
index |
index |
索引 |
primary key |
primary key |
主键,MongoDB自动将_id字段设置为主键 |
1. 创建库 use database_name 如果database_name数据库存在便进入该数据库中,如果不存在创建该数据库并进入数据库中。
> use database_name
switched to db database_name
2. 删除库 dropDatabase() 来删除(删除需慎重)
删除前:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mysql 0.000GB
test 0.000GB
删除:
> use mysql
switched to db mysql
> db
mysql
> db.dropDatabase()
{
"dropped" : "mysql", "ok" : 1 }
删除后:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
3. 创建集合(sql中表的概念)db.createCollection()
> use dbtest
switched to db dbtest
> show tables
> db.createCollection('a1')
{
"ok" : 1 }
> show tables
a1
4. 删除集合(sql中表的概念)db.tables_name.drop()
> show tables
a1
> db.a1.drop()
true
> show tables
>
5. 插入文档document - sql新增记录行 db.table_name.insert(document)
> db
dbtest
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
> db.a1.insert({
'username':'xiaoming','age':19,'password':'123','create_time':'2020-12-09 17:00:00'})
WriteResult({
"nInserted" : 1 })
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09590c0996d1ec790c72c"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
6. 更新文档 - 更新数据记录行
db.collection.update(<query>,<update>,{
multi: <boolean>})
# query:修改的查询条件,类似于SQL中的WHERE部分
# update:更新属性的操作符,类似与SQL中的SET部分
# multi:设置为true时会更新所有符合条件的文档,默认为false只更新找到的第一条
> db
dbtest
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
> db.a1.insert({
'username':'xiaoming','age':19,'password':'123','create_time':'2020-12-09 17:00:00'})
WriteResult({
"nInserted" : 1 })
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09590c0996d1ec790c72c"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
> db.a1.update({
'username':'xiaoming'},{
$set:{
'age':20}},{
multi:true})
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09590c0996d1ec790c72c"), "username" : "xiaoming", "age" : 20, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
>
7. 删除文档 - 删除数据记录行 remove()
db.collection.remove(<query>,{
justOne: <boolean>})
# query:删除的查询条件,类似于SQL中的WHERE部分
# justOne:设置为true只删除一条记录,默认为false删除所有记录
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09590c0996d1ec790c72c"), "username" : "xiaoming", "age" : 20, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
> db.a1.remove({
'username':'xiaoming'},{
justOne:true})
WriteResult({
"nRemoved" : 1 })
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
>
8. 通过collection对象的find()方法来查询文档
mongo符号 |
含义 |
mongo格式 |
SQL语句 |
: |
等于 |
{:} |
where a = ‘b’ |
$lt |
小于 |
{:{$lt:}} |
where a < 10 |
$gt |
大于 |
{:{$gt:}} |
where a > 20 |
$lte |
小于等于 |
{:{$lte:}} |
where a <= 10 |
$gte |
大于等于 |
{:{$gte:}} |
where a >= 20 |
$ne |
不等于 |
{:{$ne:}} |
where a != 30 |
db.collection.find(query, projection)
# query:查询条件,类似于SQL中的WHERE部分
# projection:可选,使用投影操作符指定返回的键
> db.a1.find()
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- 等于
> db.a1.find({
'age':22})
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- 小于
> db.a1.find({
'age':{
$lt:20}})
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- 小于等于
> db.a1.find({
'age':{
$lte:19}})
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- 大于
> db.a1.find({
'age':{
$gt:19}})
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- 大于等于
> db.a1.find({
'age':{
$gte:343}})
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- 不等于
> db.a1.find({
'age':{
$ne:343}})
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- and
> db.a1.find({
'age':{
$ne:343},'username':'xiaoming'})
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
> db.a1.find({
'age':{
$ne:343},'username':'xiaoming1'})
>
# 按条件查询 -- or
> db.a1.find({
$or:[{
'age':{
$ne:343}},{
'username':'xiaoming'}]})
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 按条件查询 -- and+or
> db.a1.find({
'age':{
$lt:22}},{
$or:[{
'age':{
$ne:343}},{
'username':'xiaoming'}]})
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc") }
> db.a1.find({
'age':{
$gt:22}},{
$or:[{
'age':{
$ne:343}},{
'username':'xiaoming'}]})
{
"_id" : ObjectId("5fd09c967cb5fe86451090be") }
9. 其他操作
limit(指定长度数据)与skip(跳过某些数量)
> db.a1.find().limit(2)
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
> db.a1.find().limit(3).skip(2)
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
> db.a1.find().limit(3).skip(1)
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
sort(排序)升序 1,降序 -1
# 升序
db.a1.find().sort({
'age':1})
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
# 降序
> db.a1.find().sort({
'age':-1})
{
"_id" : ObjectId("5fd09c967cb5fe86451090be"), "username" : "xiaoming", "age" : 343, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09c8c7cb5fe86451090bd"), "username" : "xiaoming", "age" : 22, "password" : "1234", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd09b0b7cb5fe86451090bc"), "username" : "xiaoming", "age" : 19, "password" : "123", "create_time" : "2020-12-09 17:00:00" }
{
"_id" : ObjectId("5fd092ffc0996d1ec790c72a") }
索引 createIndex()
db.collection.createIndex(keys, options)
# background:建索引过程会阻塞其它数据库操作,设置为true表示后台创建,默认为false
# unique:设置为true表示创建唯一索引
# name:指定索引名称,如果没有指定会自动生成
# {
'age':1} 1 :升序; -1 :降序
# 普通索引
> db.a1.createIndex({
'age':1},{
background:true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
# 唯一索引
> db.a1.createIndex({
'_id':1},{
unique:true})
{
"ok" : 0,
"errmsg" : "The field 'unique' is not valid for an _id index specification. Specification: { ns: \"dbtest.a1\", v: 2, key: { _id: 1.0 }, name: \"_id_1\", unique: true }",
"code" : 197,
"codeName" : "InvalidIndexSpecificationOption"
}
# 查看索引
> db.a1.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "dbtest.a1"
},
{
"v" : 2,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "dbtest.a1",
"background" : true
}
]
10. 聚合函数
操作符 |
描述 |
$sum |
计算总和 |
$avg |
计算平均值 |
$min |
计算最小值 |
$max |
计算最大值 |
# 计算总和
> db.a1.aggregate([{
$group : {
_id : "$by", sum_count : {
$sum : 1}}}])
{
"_id" : null, "sum_count" : 4 }
# 计算平均值
> db.a1.aggregate([{
$group : {
_id : "$by", avg_age : {
$avg : "$age"}}}])
{
"_id" : null, "avg_age" : 128 }
11. 正则表达式
# 不区分大小写的模糊查询,使用$options操作符
db.a1.find({
title:{
$regex:"elasticsearch",$options:"$i"}})