zoukankan      html  css  js  c++  java
  • python浅学【网络服务中间件】之MongoDB

    一、关于MongoDB:

    MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。

    在高负载的情况下,添加更多的节点,可以保证服务器性能。

    MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。

    MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

    二、MongoDB的优势:

    • MongoDB 的架构较少。它是一个文档数据库,它的一个集合持有不同的文档。
    • 从一个到另一个的文档的数量,内容和大小可能有差异。
    • MongoDB 中单个对象的结构很清淅。
    • MongoDB 中没有复杂的连接。
    • MongoDB 提供深度查询的功能,因为它支持对文档的强大的动态查询。
    • MongoDB 很容易扩展。
    • 它使用内部存储器来存储工作集,这是其快速访问的原因。

    三、MongoDB的简单使用:

    连接MongoDB shell:

    mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

    • mongodb:// 这是固定的格式,必须要指定。

    • username:password@ 可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登陆这个数据库

    • host1 必须的指定至少一个host, host1 是这个URI唯一要填写的。它指定了要连接服务器的地址。如果要连接复制集,请指定多个主机地址。

    • portX 可选的指定端口,如果不填,默认为27017

    • /database 如果指定username:password@,连接并验证登陆指定数据库。若不指定,默认打开 test 数据库。

    • ?options 是连接选项。如果不使用/database,则前面需要加上/。所有连接选项都是键值对name=value,键值对之间通过&或;(分号)隔开

    以下以windows为例:

    PS D:MongoDBServer4.2in> .mongo.exe                                                      
    MongoDB shell version v4.2.4-14-g88053fe                                                       
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb       
    Implicit session: session { "id" : UUID("6dd2f853-83f7-4709-beea-0a78e3e49364") }              
    MongoDB server version: 4.2.4-14-g88053fe                                                      
    Server has startup warnings:                                                                   
    2020-03-21T12:05:41.197+0800 I  CONTROL  [initandlisten]                                       
    2020-03-21T12:05:41.197+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enab
    led for the database.                                                                          
    2020-03-21T12:05:41.197+0800 I  CONTROL  [initandlisten] **          Read and write access to d
    ata and configuration is unrestricted.                                                         
    2020-03-21T12:05:41.197+0800 I  CONTROL  [initandlisten]                                       
    ---                                                                                            
    Enable MongoDB's free cloud-based monitoring service, which will then receive and display      
    metrics about your deployment (disk utilization, CPU, operation statistics, etc).              
                                                                                                   
    The monitoring data will be available on a MongoDB website with a unique URL accessible to you 
    and anyone you share the URL with. MongoDB may use this information to make product            
    improvements and to suggest MongoDB products and deployment options to you.                    
                                                                                                   
    To enable free monitoring, run the following command: db.enableFreeMonitoring()                
    To permanently disable this reminder, run the following command: db.disableFreeMonitoring()    
    ---                                                                                            
                                                                                                                                                                                   
    > show dbs  # 查看当前数据库                                                                              
    admin   0.000GB   # 数据库帐号信息就存储在admin数据库                                                                            
    config  0.000GB   # 存储副本集的配置信息                                                                               
    local   0.000GB   # 在本地存储数据                                                                                
    > use test  # 创建数据库                                                                                
    switched to db test                                                                            
    > show dbs  # 查看数据库发现没有显示,因为里面没有数据                                                                               
    admin   0.000GB                                                                              
    config  0.000GB                                                                                
    local   0.000GB                                                                             
    > db.test.insert({'key':'value'})  # 插入数据                                                     
    WriteResult({ "nInserted" : 1 })                                                               
    > show dbs  # 再次查看数据库,发现 test 数据库显示                                                                              
    admin   0.000GB                                                                                
    config  0.000GB                                                                                
    local   0.000GB                                                                                
    test    0.000GB                                                                                                                                                                                                                                                 
    > db.test.drop()  # 删除数据库                                                                          
    true                                                                                           
    > show dbs                                                                                     
    admin   0.000GB                                                                                
    config  0.000GB                                                                                
    local   0.000GB                                                                                
    > db.createCollection('gather')  # 创建一个集合                                                          
    { "ok" : 1 }                                                                                   
    > show collections  # 查看集合                                                                     
    gather
    > db.gather.insert({name:'riy',sex:'boy',mood:'haha'})  # 集合中插入数据                                     
    WriteResult({ "nInserted" : 1 })
    > db.gather.find()  # 查看集合数据                                                                
    { "_id" : ObjectId("5e787d761f3d1841d88ce3f1"), "name" : "riy", "sex" : "boy", "mood" : "haha" 
    }
    > db.gather.update({'sex':'boy'},{$set:{'sex':'girl'}})  # 修改集合中的数据                       
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })                              
    > db.gather.find()                                                                             
    { "_id" : ObjectId("5e787d761f3d1841d88ce3f1"), "name" : "riy", "sex" : "girl", "mood" : "haha"
     }                                                                                                
    > db.gather.drop()  # 删除集合                                                                      
    true                                                                                                                                                                              
                                                                                                                                                         

    四、python简单操作MongoDB:

    安装pymongo包: pip install pymongo

    计算一个库中所有的 collection 数

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Riy
    
    
    import pymongo
    
    
    myclient = pymongo.MongoClient('mongodb://localhost:27017/')    # 创建连接
    mydb = myclient["test"]  # 库名
    coll_names = mydb.list_collection_names()  # 获取所有collection
    k = 0
    for i in coll_names:
        count = mydb[i].find().count()
        k += count
    print(k)  # 计数
  • 相关阅读:
    第一册:lesson 117.
    第一册:lesson 115.
    Map集合。
    第一册:lesson 113.
    第一册:lesson 111.
    泛型。
    EXT.NET初学
    LINQ查询
    后台调用前端JS
    数字与数组或者字符串里面的内容比较
  • 原文地址:https://www.cnblogs.com/riyir/p/12553710.html
Copyright © 2011-2022 走看看