基础:
- 是非关系型数据库
- 文档存储
1、显示当前的所有数据库
- show dbs
- show databases;
2、使用数据库
- use 数据库
3、查看当前数据库
- db
4、显示数据库中所有的集合
- show collections
5、查看docker日志
- docker logs docker_mongodb(mongodb的docker容器名字)
6、Mongo Express是一个基于网络的MongoDB数据库管理界面
- docker pull mongo-express // 下载mongo-express镜像
- docker run --link docker_mongodb:mongo -p 8081:8081 mongo-express //运行mongo-express,docker_mongodb是docker中mongodb容器名字
7、mongo shell是用来操作MongoDB的javascript客户端界面
- 运行mogog shell
docker exec -it docker_mongodb mongo - exit 退出
8、数据库的CRUD(增删改查)操作
- Create、Read、Update、Delete
- 文档主键: _id
- 文档主建有唯一性
- 支出所有数据类型(除了数组)
- 自动生成主建(12字节,前四个是时间)
9、创建文档
- db.collection.insert()
1、db.insertOne() 一次只能插入一个文档db.accounts.insertOne({name:"nanzai", balance: 3500}) { "acknowledged" : true, "insertedId" : ObjectId("5e157856665014c2c50ea538") }
2、db.insertMany()必须插入多个文档db.accounts.insertMany([{name:"xinzai", balance: 500}, {name:"xinge", balance:600}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5e1576cc665014c2c50ea536"), ObjectId("5e1576cc665014c2c50ea537") ] }
3、db.collection.inset()可以插入一个或者多个文档db.accounts.insert([{name: "jack",balance: 2000, contact: ["137111111","陕西省","China"]},{name: "karen", balance: 3000, contact: ["18391000411", "湖南省", "china"]}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
db.accounts.insert({name: "ke", balance: 1800, "contact": [["1754684641","195324664654"], "陕西省", "China"]}) WriteResult({ "nInserted" : 1 })
- db.collection.save()
- 创建多个文档
10、查找文档
数组操作符:all
- db.collection.find()查询所有文档
db.accounts.find() { "_id" : ObjectId("5e1331591f342ac67bb98e52"), "name" : "xiaoke", "balance" : 1000 } { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e13331e1f342ac67bb98e54"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e1336e91f342ac67bb98e57"), "name" : "jack", "balance" : 2000, "contact" : [ "137111111", "陕西省", "China" ] } { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e59"), "name" : "xiaoke", "balance" : 1000, "contact" : [ "1371811111", "山西省", "China" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e5a"), "name" : "xiaowang", "balance" : 1500, "contact" : [ "18891000411", "湖南省", "china" ] }
- 读取联系地址位于China陕西省的银行账户信息
db.accounts.find({contact:{$all:["China","陕西省"]}}) { "_id" : ObjectId("5e1336e91f342ac67bb98e57"), "name" : "jack", "balance" : 2000, "contact" : [ "137111111", "陕西省", "China" ] }
- 读取联系电话包含1754684641和195324664654的银行账户文档
db.accounts.find( { "contact":{ $all: [["1754684641", "195324664654"]]}}) { "_id" : ObjectId("5e133b15738962fb118b2554"), "name" : "ke", "balance" : 1800, "contact" : [ [ "1754684641", "195324664654" ], "陕西省", "China" ] }
-
.pretty()打印的更加清楚
db.accounts.find({name: "xin"}).pretty() { "_id" : ObjectId("5e154a6d297892ae4951650d"), "name" : "xin", "balance" : 5000, "contact" : [ "1204557644", "广东省", "china" ] } { "_id" : ObjectId("5e154a79297892ae4951650e"), "name" : "xin", "balance" : 5000, "contact" : [ "1204557644", "广东省", "china" ] }
elemMatch 任一操作符
- 读取联系电话范围在“1371811111”和“195324664654”之间的银行账户文档
db.accounts.find( {contact: {$elemMatch: {$gt:"1371811111", $lt: "195324664654"}}}) { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e5a"), "name" : "xiaowang", "balance" : 1500, "contact" : [ "18891000411", "湖南省", "china" ] }
正则表达式
- 在和$in操作符一起使用时,只能使用/pattern/<options>
- 读取用户姓名以k或者j开头的银行账户文档
db.accounts.find({name: {$in : [/^k/, /^j/]}}) { "_id" : ObjectId("5e1336e91f342ac67bb98e57"), "name" : "jack", "balance" : 2000, "contact" : [ "137111111", "陕西省", "China" ] } { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] } { "_id" : ObjectId("5e133b15738962fb118b2554"), "name" : "ke", "balance" : 1800, "contact" : [ [ "1754684641", "195324664654" ], "陕西省", "China" ] }
文档游标
- db.collection.find()返回一个文档集合游标
- 在不迭代游标的情况下,只列出前20个文档
- var myCour = db.accounts.find()
myCourvar myCourt = db.accounts.find() > myCourt { "_id" : ObjectId("5e1331591f342ac67bb98e52"), "name" : "xiaoke", "balance" : 1000 } { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e13331e1f342ac67bb98e54"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e1336e91f342ac67bb98e57"), "name" : "jack", "balance" : 2000, "contact" : [ "137111111", "陕西省", "China" ] } { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e59"), "name" : "xiaoke", "balance" : 1000, "contact" : [ "1371811111", "山西省", "China" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e5a"), "name" : "xiaowang", "balance" : 1500, "contact" : [ "18891000411", "湖南省", "china" ] } { "_id" : ObjectId("5e133b15738962fb118b2554"), "name" : "ke", "balance" : 1800, "contact" : [ [ "1754684641", "195324664654" ], "陕西省", "China" ] }
- 我们也可以使用游标下标直接访问文档集合中的某一个文档
var myCourt = db.accounts.find() > myCourt[1] { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 }
- 游历完游标中所有的文档之后,或者在10分钟之后,游标便会自动关闭。
- 可以使用noCursorTimeout()函数来保持游标一直有效。
var myCursor = db.accounts.find().noCursorTimeout() myCursor.close //关闭游标
- 遍历所有文档游标
方法一:var my = db.accounts.find({ name : "one"}); > while( my.hasNext()){ printjson(my.next()) } { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e13331e1f342ac67bb98e54"), "name" : "one", "balance" : 1000 }
方法二:
var my = db.accounts.find({ name : "one"}); > my.forEach(printjson) { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e13331e1f342ac67bb98e54"), "name" : "one", "balance" : 1000 }
- cursor.limit(<number>)查询指定数量
db.accounts.find().limit(3) { "_id" : ObjectId("5e1331591f342ac67bb98e52"), "name" : "xiaoke", "balance" : 1000 } { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e13331e1f342ac67bb98e54"), "name" : "one", "balance" : 1000 }
find().limit(0)和find()结果一样。
- cursor.skip(<offset>)绕过指定数量
db.accounts.find().skip(3) { "_id" : ObjectId("5e1336e91f342ac67bb98e57"), "name" : "jack", "balance" : 2000, "contact" : [ "137111111", "陕西省", "China" ] } { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e59"), "name" : "xiaoke", "balance" : 1000, "contact" : [ "1371811111", "山西省", "China" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e5a"), "name" : "xiaowang", "balance" : 1500, "contact" : [ "18891000411", "湖南省", "china" ] } { "_id" : ObjectId("5e133b15738962fb118b2554"), "name" : "ke", "balance" : 1800, "contact" : [ [ "1754684641", "195324664654" ], "陕西省", "China" ] }
- curor.count(<applySkipLimit>)
默认情况下,<applySkipLimit>为false,既cursor.count()不会考虑cursor.skip()和cursor.limit()的效果db.accounts.find().limit(1).count() 8 > db.accounts.find().limit(1).count(true) 1
- cursor.sort(<document>)
这里的<document>定义了排序的要求, 1表示由小及大的正向排序, -1 为逆向序 - 按照余额从大到小,用户姓名按字母排序的方式排列银行账户文档
db.accounts.find().sort({balance: -1, name: 1}) { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] } { "_id" : ObjectId("5e1336e91f342ac67bb98e57"), "name" : "jack", "balance" : 2000, "contact" : [ "137111111", "陕西省", "China" ] } { "_id" : ObjectId("5e133b15738962fb118b2554"), "name" : "ke", "balance" : 1800, "contact" : [ [ "1754684641", "195324664654" ], "陕西省", "China" ] } { "_id" : ObjectId("5e1337dc1f342ac67bb98e5a"), "name" : "xiaowang", "balance" : 1500, "contact" : [ "18891000411", "湖南省", "china" ] } { "_id" : ObjectId("5e1332d11f342ac67bb98e53"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e13331e1f342ac67bb98e54"), "name" : "one", "balance" : 1000 } { "_id" : ObjectId("5e1331591f342ac67bb98e52"), "name" : "xiaoke", "balance" : 1000 } { "_id" : ObjectId("5e1337dc1f342ac67bb98e59"), "name" : "xiaoke", "balance" : 1000, "contact" : [ "1371811111", "山西省", "China" ] }
- 读取余额最大的银行账户文档
<db.accounts.find().sort({balance:-1}).limit(1) { "_id" : ObjectId("5e1336e91f342ac67bb98e58"), "name" : "karen", "balance" : 3000, "contact" : [ "18391000411", "湖南省", "china" ] }