玩转MongoDB4.0(MongoDB基础总结)

基础:

  • 是非关系型数据库
  • 文档存储

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()
       myCour
     var 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" ] }
    
    <
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值