創(chuàng)建mongodb
服務(wù)
我們這里使用docker-compose
編排文件的形式創(chuàng)建
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
先進(jìn)入容器內(nèi)部,運(yùn)行命令mongo
犀填,進(jìn)入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
則代表登錄成功。
為數(shù)據(jù)庫(kù)創(chuàng)建用戶
> db.createUser({user: "username", pwd: "password", roles: [{ role: "dbOwner", db: "dbname" }]})
Successfully added user: {
"user" : "manage",
"roles" : [
{
"role" : "dbOwner",
"db" : "dbname"
}
]
}
選擇操作的數(shù)據(jù)庫(kù)
>use dbname
switched to db dbname
這里的dbname
替換成自己的數(shù)據(jù)庫(kù)名稱
用戶名密碼登錄
> db.auth("username", "password")
1
>
將這里的username
和password
替換成該數(shù)據(jù)庫(kù)的用戶名密碼九巡,返回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
命令是創(chuàng)建索引,其中的collectionName
是集合名稱冕广,param
是數(shù)據(jù)中的參數(shù)疏日,為1
時(shí)創(chuàng)建的是正序的索引,為-1
創(chuàng)建的是逆序的索引撒汉。db.col.getIndexes()
命令是獲取當(dāng)前數(shù)據(jù)庫(kù)的所有索引沟优。
所有索引列表
> 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"
}
]
>