1. 创建用户
// 管理员用户 db:admin
db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
//应用数据库用户 db:privatenodes
db.createUser({user: "zxl",pwd: "123456",roles: [ { role: "readWrite", db: "privatenodes" } ]});
2. 创建集合
use privatenodes;
db.createCollection("networks");
3. 创建索引
use privatenodes;
// 基础索引
db.networks.createIndex({"networkid":1});
// 唯一索引
db.networks.ensureIndex({"networkid":1, "platformcode":1,"state":1,"region":1}, {unique: true});
// 删除索引
db.networks.dropIndex({"networkid":1})
4.数据操作
// 插入数据
db.networks.insert(
{
"networkId": "ddd1",
"create": {
"req": {
"networkId": "ddd1",
"platformCode": "aws"
},
"res": {
"hostStatus": 1,
"hostStatusMsg": "",
"deployStatus": 1,
"deployStatusMsg": "dd",
"hosts": [{
"wanIP": "192.168.1.173"
}, {
"wanIP": "192.168.1.171"
}]
}
},
"delete": {
"req": {
"networkId": "ddd",
"platformCode": "aws "
},
"res": {
"status": 1,
"statusMsg": ""
}
}
});
// 查找数据
db.networks.find({ networkId:"ddd"})
// 删除数据
db.networks.deleteOne({ networkId:"ddd1"})
// 清空数据
db.networks.drop()
网友评论