Last active
January 17, 2019 23:33
-
-
Save shau-lok/f2eb09696fe6bf5c337a40ae2a82a38d to your computer and use it in GitHub Desktop.
docker-mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker pull mongo:3.0.15-wheezy | |
$ docker run --name mongo -itd -p 27017:27017 mongo:3.0.15-wheezy --auth | |
# 增加admin管理账号 | |
$ docker exec -it mongo mongo admin | |
> use admin | |
switched to db admin | |
> db.createUser( | |
... { | |
... user: "sailear", | |
... pwd: '123456', | |
... roles: [ { role: 'userAdminAnyDatabase', db: 'admin'}] | |
... }) | |
Successfully added user: { | |
"user" : "admin", | |
"roles" : [ | |
{ | |
"role" : "userAdminAnyDatabase", | |
"db" : "admin" | |
} | |
] | |
} | |
# 创建其他库的user | |
$ mongo | |
> use admin | |
> db.auth("sailear", "123456") | |
> use test | |
> db.createUser( { user: "shau-lok", pwd: "123456", roles: [ {role: "readWrite", db: "test"}]}) | |
# 登陆 | |
$ mongo -u shau-lok -p 123456 --authenticationDatabase "test" | |
# CRUD | |
# 插入 | |
> db.users.insertOne({name: "sue", age:25, status:"pending"}); | |
> db.foo.insert({x:1, y:1}) | |
> db.products.insertMany( [ | |
{ item: "card", qty: 15 }, | |
{ item: "envelope", qty: 20 }, | |
{ item: "stamps" , qty: 30 } | |
] ); | |
# 读取 | |
> db.users.find() | |
> db.users.find( {age: {$gt: 18}}, {name:1}).limit(5) | |
# 查询是否在列表里边 | |
mongodb数据为 | |
{"directors" : [ | |
"塞巴斯蒂安·莱里奥" | |
]} | |
> db.getCollection('movie').find({'directors': { '$in': ['塞巴斯蒂安·莱里奥']}}) | |
> db.getCollection('douban_movie').find({'types': { '$in': ['喜剧']}, 'rate': { '$gt': "8" }}).sort({'rate': -1}) | |
创建唯一索引 | |
> db.collection.ensureIndex( { "username": 1 }, { unique: true } ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment