MongoDB入门

MongoDB入门

1、什么是NoSQL

NoSQL是和传统的关系型数据库不同的数据库的统称。

关系型数据库的代表有MySQL,Oracle,Microsoft SQL Server。

NoSQL的代表有MongoDB,Memcached,Redis。

2、NoSQL分类

NoSQL分四类:键值存储,文档存储,图像存储,列存储。

其中,MongoDB属于文档存储,而Memcached和Redis属于内存级别的键值存储。

3、MongoDB简介

MongoDB是一个开源,高性能,并给予分布式文件存储的文档型数据库,

4、MongoDB特性

1、面向集合存储,易存储对象类型的数据
2、模式自由
3、支持动态查询
4、支持完全索引,包含内部对象
5、支持查询
6、支持复制和故障恢复
7、使用二进制数据存储,可以存储大型的对象(如图片,视频等)
8、自动处理碎片
9、支持多种语言
10、文档存储格式是BSON
11、网络访问

5、MongoDB安装

1、环境准备

系统:CentOS7

MongoDB:3.2.8

2、安装

创建一个目录,用于存放MongoDB的文件

mkdir /software/mongodb
cd /software/mongodb

使用wget下载安装包

wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.8.tgz

使用tar解压

tar zxvf mongodb-linux-x86_64-rhel62-3.2.8.tgz

再创建一个data文件夹和一个log文件夹

mkdir /software/mongodb/mongo/data
mkdir /software/mongodb/mongo/log
chmod 777 /software/mongodb/mongo/data
chmod 777 /software/mongodb/mongo/log

在bin目录下创建一个mongodb.conf文件,编辑内容:

#数据文件目录
dbpath = /software/mongodb/mongo/data
#日志目录
logpath = /software/mongodb/mongo/log/mongodb.log
#端口
port = 27017
#后台运行
fork = true
nohttpinterface = true

在/lib/systemd/system目录下创建一个mongodb.service文件,编辑内容:

[Unit]  
Description=mongodb  
After=network.target remote-fs.target nss-lookup.target  
  
[Service]  
Type=forking  
ExecStart=/software/mongodb/mongo/bin/mongod --config /software/mongodb/mongo/bin/mongodb.conf  
ExecReload=/bin/kill -s HUP $MAINPID  
ExecStop=/software/mongodb/mongo/mongod --shutdown --config /software/mongodb/mongo/bin/mongodb.conf  
PrivateTmp=true  
  
[Install]  
WantedBy=multi-user.target

并赋予权限

chmod 754 mongodb.service

3、运行

启动mongodb服务

systemctl start mongodb.service

启动mongodb

./mongo

控制台输出

[root@iz2zeaf5jdjve80rjlsjgnz bin]# ./mongo
MongoDB shell version: 3.2.8
connecting to: test
Server has startup warnings: 
2019-12-06T09:21:36.266+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-12-06T09:21:36.266+0800 I CONTROL  [initandlisten] 
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] 
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] 
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-12-06T09:21:36.267+0800 I CONTROL  [initandlisten] 
> 

6、MongoDB操作

1、数据库

MongoDB默认数据库是test。如果没有创建数据库,默认对test数据库进行操作。

【查看所有数据库】

> show dbs
local  0.000GB

【创建/选择数据库】

> use mydb
switched to db mydb
> show dbs
local  0.000GB
> db
mydb

创建数据库后,使用show dbs会发现没有创建的数据库,这是因为还没有往新创建的数据库里面添加数据。

添加一条数据

> db.collection.insert({"name":"rlxy93"})
WriteResult({ "nInserted" : 1 })
> show dbs
local  0.000GB
mydb   0.000GB

【查看所有集合】

插入一条数据时,如果指定的collection没有创建,那么会自动创建。

> show collections
collection

【删除数据库】

如果没有使用use命令,则默认删除test数据库,如果指定了,删除正在使用的那个数据库。

> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }

2、集合Collection

集合类似于关系型数据库中的表,一张表对应着MongoDB中的一个集合。

【创建集合】

db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: <number>, max <number>} )

其中,

name:集合名

capped:是否启动集合限制

autoIndexId:是否使用_Id作为索引,默认是true

size:集合使用的存储空间大小

max:集合的最大条数,默认是无限
> db.createCollection("my_collection")
{ "ok" : 1 }

【删除集合】

删除成功返回true,删除失败返回false

> show collections
my_collection
> db.my_collection.drop()
true
> show collections

【插入数据】

insert():将一条或多条数据插入到指定的Collection中

插入一条时,直接用{}括起来,插入多条时,需要用[]把{}的集合括起来组成一个数组

> db.createCollection("my_collection")
{ "ok" : 1 }
> db.my_collection.insert({"name":"rlxy93","address":"chongqing"})
WriteResult({ "nInserted" : 1 })
> db.my_collection.insert([{"name":"a","address":"chongqing"},{"name":"b","address":"chongqing"}])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c15f7be63bdb51ddcd6d"), "name" : "a", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }


insertOne():将一条数据插入到指定的Collection中

插入后,MongoDB会自动生成该条文档的一个_id

> db.my_collection.insertOne({"name":"lxy","address":"sichuan"})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5de9c2777be63bdb51ddcd70")
}
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }

insertMany():将多条数据插入到指定的Collection中

> db.my_collection.insertMany([{"name":"c","address":"chongqing"},{"name":"d","address":"chongqing"}])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5de9c3167be63bdb51ddcd71"),
                ObjectId("5de9c3167be63bdb51ddcd72")
        ]
}

【查询数据】

db.my_collection.find([query],[fields])
db.my_collection.find(...).count()
db.my_collection.find(...).limit(n)
db.my_collection.find(...).skip(n)
db.my_collection.find(...).sort(...)

其中,

query:过滤条件

fields:需要查询返回的字段

count():统计查询到的数量

limit(n):显示多少条数据

skip(n):跳过多少条

sort():排序

【查询集合的所有数据】

> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }

【根据条件查询集合的数据】

> db.my_collection.find({"name":"a"})
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "chongqing" }

【根据条件查询集合的数据,并返回指定字段】

要显示哪个字段,需要在第二个参数设置 字段名:1

1表示显示,0表示不显示。

> db.my_collection.find({"name":"a"},{"address":1})
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "address" : "chongqing" }

【统计查询到的数量】

> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "chongqing" }
> db.my_collection.find().count()
7

【显示指定条数】

> db.my_collection.find().limit(2)
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "chongqing" }

【跳过指定条数】

> db.my_collection.find().skip(2)
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "chongqing" }

【按照指定字段排序】

> db.my_collection.find().sort({"name":1})
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "chongqing" }

【更新数据】

db.my_collection.updateOne( filter, update, <optional params> )
db.my_collection.updateMany( filter, update, <optional params> )
db.my_collection.replaceOne( filter, replacement, <optional params> )
db.my_collection.update( query, object[, upsert_bool, multi_bool] )

其中,

filter:过滤条件

update:替换的值

optional params:可选的值有upsert, w, wtimeout, j

update:是集成了updateOne,updateMany,replaceOne为一体的

【更新一条符合条件的数据】

如果update不加其他参数,默认只更改一行

> db.my_collection.updateOne({"name":"rlxy93"},{"$set":{"address":"sichuan"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "chongqing" }

【更新所有符合条件的数据】

> db.my_collection.updateMany({"name":"a"},{$set:{"address":"yongchuan"}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "a", "address" : "yongchuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }

【替换数据】

> db.my_collection.replaceOne({"name":"a"},{"name":"replaceOne"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }

【删除数据】

db.my_collection.deleteOne( filter, <optional params> )
db.my_collection.deleteMany( filter, <optional params> )
db.my_collection.remove(query)

其中,

filter:过滤条件

query:过滤条件

【删除一条数据】

> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd71"), "name" : "c", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }
> db.my_collection.deleteOne({"name":"c"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }

【删除多条数据】

> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6f"), "name" : "b", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c3167be63bdb51ddcd72"), "name" : "d", "address" : "chongqing" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }
> db.my_collection.deleteMany({"address":"chongqing"})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }

【删除多条数据】

> db.my_collection.find()
{ "_id" : ObjectId("5de9c0747be63bdb51ddcd6c"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9c7a97be63bdb51ddcd73"), "name" : "rlxy93", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }
> db.my_collection.remove({"name":"rlxy93"})
WriteResult({ "nRemoved" : 2 })
> db.my_collection.find()
{ "_id" : ObjectId("5de9c1a27be63bdb51ddcd6e"), "name" : "replaceOne" }
{ "_id" : ObjectId("5de9c2777be63bdb51ddcd70"), "name" : "lxy", "address" : "sichuan" }
{ "_id" : ObjectId("5de9d0f37be63bdb51ddcd74"), "name" : "a", "address" : "yongchuan" }

7、总结

通过以上笔记,能够学到NoSQL的一些概念,还有MongoDB的特点,安装等,并学会了MongoDB的基本CRUD操作。

原文地址:https://www.cnblogs.com/lxxxxxxy/p/12001315.html