创建mongodb
服务
我们这里使用docker-compose
编排文件的形式创建
version: '3'
services:
mongodb:
image: mongo:3.0-wheezy
container_name: mongo
volumes:
- ./data:/data/db
- /etc/localtime:/etc/localtime:ro
restart: always
ports:
- "27017:27017"
logging:
driver: "json-file"
options:
max-size: "200k"
max-file: "10"
environment:
MONGO_INITDB_ROOT_USERNAME: root_username //初始化root用户名
MONGO_INITDB_ROOT_PASSWORD: root_password //初始化root密码
登录Mongo
先进入容器内部,运行命令mongo
,进入mongodb
的shell
root@ubuntu-70TJA00QCN:~# docker exec -it mongo /bin/bash
root@df2e73225af9:/# ls
bin boot data dev docker-entrypoint-initdb.d etc home js-yaml.js lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@df2e73225af9:/# mongo
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("62306e90-5429-4f00-90c5-70e1ecb68915") }
MongoDB server version: 4.0.6
>
root用户登录
> db.auth("root_username", "root_password")
1
>
将这里的root_username
和root_password
替换成root用户名密码,返回1
则代表登录成功。
为数据库创建用户
> db.createUser({user: "username", pwd: "password", roles: [{ role: "dbOwner", db: "dbname" }]})
Successfully added user: {
"user" : "manage",
"roles" : [
{
"role" : "dbOwner",
"db" : "dbname"
}
]
}
选择操作的数据库
>use dbname
switched to db dbname
这里的dbname
替换成自己的数据库名称
用户名密码登录
> db.auth("username", "password")
1
>
将这里的username
和password
替换成该数据库的用户名密码,返回1
则代表登录成功。
添加索引
> db.collectionName.createIndex({"param1":1,"param2":1,"param3":1,"param4":1})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.collectionName.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "dbname.collectionName"
},
{
"v" : 2,
"key" : {
"param1" : 1,
"param2" : 1,
"param3" : 1,
"param4" : 1
},
"name" : "param1_1_param2_1_param3_1_param4_1",
"ns" : "dbname.collectionName"
}
]
>
db.collectionName.createIndex
命令是创建索引,其中的collectionName
是集合名称,param
是数据中的参数,为1
时创建的是正序的索引,为-1
创建的是逆序的索引。db.col.getIndexes()
命令是获取当前数据库的所有索引。
所有索引列表
> db.udpdata.createIndex({"dataTypeId":1,"deviceName":1,"productKey":1,"timeStamp":1})
> db.udpdata.createIndex({"dataTypeId":1,"deviceName":1,"productKey":1,"timeStamp":-1})
> db.udpdata.createIndex({"deviceName":1,"timeStamp":-1})
> db.udpdata.createIndex({"dataTypeId":1,"deviceName":1,"productKey":1,"data.status":1})
> db.udpdata.createIndex({"dataTypeId":1,"requestId":1})
> db.udpdata.createIndex({"dataTypeId":1,"periodId":1})
> db.udpdata.createIndex({"applicationId":1,"dataTypeId":1,"timeStamp":1})
> db.udpdata.createIndex({"dataTypeId":1,"timeStamp":1})
> db.udpdata.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"dataTypeId" : 1,
"deviceName" : 1,
"productKey" : 1,
"timeStamp" : 1
},
"name" : "dataTypeId_1_deviceName_1_productKey_1_timeStamp_1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"dataTypeId" : 1,
"deviceName" : 1,
"productKey" : 1,
"timeStamp" : -1
},
"name" : "dataTypeId_1_deviceName_1_productKey_1_timeStamp_-1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"deviceName" : 1,
"timeStamp" : -1
},
"name" : "deviceName_1_timeStamp_-1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"dataTypeId" : 1,
"deviceName" : 1,
"productKey" : 1,
"data.status" : 1
},
"name" : "dataTypeId_1_deviceName_1_productKey_1_data.status_1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"dataTypeId" : 1,
"requestId" : 1
},
"name" : "dataTypeId_1_requestId_1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"dataTypeId" : 1,
"periodId" : 1
},
"name" : "dataTypeId_1_periodId_1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"applicationId" : 1,
"dataTypeId" : 1,
"timeStamp" : 1
},
"name" : "applicationId_1_dataTypeId_1_timeStamp_1",
"ns" : "manage-iot-hub.udpdata"
},
{
"v" : 2,
"key" : {
"dataTypeId" : 1,
"timeStamp" : 1
},
"name" : "dataTypeId_1_timeStamp_1",
"ns" : "manage-iot-hub.udpdata"
}
]
>