Docker run mongodb
docker run --name some-mongo -d mongo
进入容器
docker exec -ti some-mongo bash
容器中连接 mongo 服务
root@88ec7a943cb7:/data/db# mongo MongoDB shell version v3.4.10 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.10 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2017-11-30T03:05:22.253+0000 I STORAGE [initandlisten] 2017-11-30T03:05:22.253+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2017-11-30T03:05:22.253+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-11-30T03:05:22.320+0000 I CONTROL [initandlisten] > db test
本地连接服务器数据库
默认端口号为 27017. mongo localhost:port/db -u username -p password
mongo 172.10.10.10:27017/db1 -u admin -p admin
Docker 运行的 mongo 进行 shell 交互模式根目录连接服务器数据库
root@fd3e075b3549:/# mongo 172.10.10.10:27017/db1 -u username -p password MongoDB shell version v3.6.4 connecting to: mongodb://172.10.10.10:27017/db1 MongoDB server version: 3.4.9 WARNING: shell and server versions do not match > db db1
Docker 容器中使用管理员用户(admin)连接,连接成功后创建对应数据库以及对应的用户
$ docker exec -ti some-mongo bash root@c6a7b5196896:/# mongo 172.10.10.18:27017/admin -u admin MongoDB shell version v4.0.3 Enter password: connecting to: mongodb://172.10.10.18:27017/admin WARNING: No implicit session: Logical Sessions are only supported on server versions 3.6 and greater. Implicit session: dummy session MongoDB server version: 3.4.9 WARNING: shell and server versions do not match Server has startup warnings: 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine. 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems: 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options] 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-11-08T13:50:06.721+0000 I CONTROL [initandlisten] >
切换到 admin 数据库下
> use admin switched to db admin
登录 admin 用户
> db.auth('admin','123456') 1
查看全局所有账户
> db.system.users.find().pretty() { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "MONGODB-CR" : "b5097e4b94862cf48c95exxxxxxx" }, "roles" : [ { "role" : "root", "db" : "admin" } ] } { "_id" : "admin.user_test", "user" : "user_test", "db" : "admin", "credentials" : { "MONGODB-CR" : "cf14962de9c8705afef86xxxxxx" }, "roles" : [ { "role" : "readWrite", "db" : "user_test" } ] }
创建数据库
use DATABASE_NAME
数据库不存在则创建数据库,否则切换到指定数据库
> use runoob switched to db runoob > db runoob >
查看当前数据库下的所有用户
> use db1 switched to db db1 > show users { "_id" : "db1.user_test", "user" : "user_test", "db" : "db1", "roles" : [ { "role" : "readWrite", "db" : "db1" } ] }
创建用户
use db1 db.createUser({ "user" : "db1", "pwd" : "xxxxxx", "roles" : [ { role: "readWrite", db: "db1" } ] }) db.createUser({ "user" : "test", "pwd" : "xxxxxx", "roles" : [ { role: "read", db: "db1" } ] })
查看所有数据库
> show dbs local 0.078GB test 0.078GB >
插入数据
可以看到,我们刚创建的数据库 runoob 并不在数据库的列表中, 要显示它,我们需要向 runoob 数据库插入一些数据。
> db.runoob.insert({"name":"菜鸟教程"}) WriteResult({ "nInserted" : 1 }) > show dbs local 0.078GB runoob 0.078GB test 0.078GB >
删除数据库
db.dropDatabase()
删除当前数据库,默认为 test, 可以使用 db 命令查看当前数据库名称
查询集合
show tables
创建集合
db.createCollection(name, options)
name 创建的集合的名称
options (可选) 指定有关内存大小和索引的选项
删除集合
db.collection.drop()
向集合中插入文档
db.COLLECTION_NAME.insert(document)
> show dbs admin 0.000GB local 0.000GB runoob 0.000GB > use runoob switched to db runoob > db runoob > show tables col > db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库'}) WriteResult({ "nInserted" : 1 })
查看已插入的文档
col 是集合名,如果该集合不在该数据库中, MongoDB 会自动创建该集合并插入文档
db.collection.find(query, projection) query :可选,使用查询操作符指定查询条件 projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)
pretty() 方法以格式化的方式来显示所有文档
db.col.find().pretty()
更新文档
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } ) 参数说明: query : update的查询条件,类似sql update查询内where后面的。 update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的 upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。 multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。 writeConcern :可选,抛出异常的级别。
> db.col.find() { "_id" : ObjectId("5a13f8db577a5fb9bfdbec22") } { "_id" : ObjectId("5a14eec1619ae743f101f2b5"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库" } { "_id" : ObjectId("5a150f72619ae743f101f2b6"), "title" : "Mo", "description" : "Nosql" } > db.col.update({"title": "Mo"},{$set: {"title": "oo"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.col.find() { "_id" : ObjectId("5a13f8db577a5fb9bfdbec22") } { "_id" : ObjectId("5a14eec1619ae743f101f2b5"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库" } { "_id" : ObjectId("5a150f72619ae743f101f2b6"), "title" : "oo", "description" : "Nosql" }
更新多条数据
>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})
替换
save() 方法通过传入的文档来替换已有文档
db.collection.save( <document>, { writeConcern: <document> } ) 参数说明: document : 文档数据。 writeConcern :可选,抛出异常的级别。
> db.col.find() { "_id" : ObjectId("5a13f8db577a5fb9bfdbec22") } { "_id" : ObjectId("5a14eec1619ae743f101f2b5"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库" } { "_id" : ObjectId("5a150f72619ae743f101f2b6"), "title" : "oo", "description" : "Nosql" } > db.col.save({"_id": ObjectId("5a14eec1619ae743f101f2b5"),'title':'aaaa','description':'balabala...'}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.col.find() { "_id" : ObjectId("5a13f8db577a5fb9bfdbec22") } { "_id" : ObjectId("5a14eec1619ae743f101f2b5"), "title" : "aaaa", "description" : "balabala..." } { "_id" : ObjectId("5a150f72619ae743f101f2b6"), "title" : "oo", "description" : "Nosql" }
删除文档
remove()函数是用来移除集合中的数据
db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> } ) 参数说明: query :(可选)删除的文档的条件。 justOne : (可选)如果设为 true 或 1,则只删除一个文档。 writeConcern :(可选)抛出异常的级别。
删除第一条找到的记录可以设置 justOne 为 1
db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
删除所有数据
db.col.remove({})