天天是晴天 發表於 2021-4-16 10:27:00

python 操作MongoDB详解

<h2><span style="font-family: &quot;comic sans ms&quot;, sans-serif">一、前言</span></h2>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">  MongoDB属于 NoSQL(非关系型数据库),是一个基于分布式文件存储的开源数据库系统。</span></p>
<p>&nbsp;</p>
<h2><span style="font-family: &quot;comic sans ms&quot;, sans-serif">二、操作 MongoDB</span></h2>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">1、安装&nbsp;pymongo</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">python 使用第三方库来连接操作 MongoDB,所以我们首先安装此库。</span></p>
<blockquote>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">pip3 install&nbsp;pymongodb</span></p>
</blockquote>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">2、连接 MongoDB</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">使用 MongoClient 类连接,以下两种参数方式都可以:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> pymongo <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient

</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 连接方式一</span>
client = MongoClient(host=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>,port=27017<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 连接方式二</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> client = MongoClient('mongodb://localhost:27017/')</span></pre>
</div>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">3、选择数据库</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">MongoDB 可以创建很多 db,指定我们需要的 db 即可</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 方式一</span>
db =<span style="color: rgba(0, 0, 0, 1)"> client.Monitor
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 方式二</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> db = client['Monitor']</span></pre>
</div>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">4、选择集合</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">db 内包含很多个集合,有点类似 mysql 这类关系型数据库中的表</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 方式一</span>
collection =<span style="color: rgba(0, 0, 0, 1)"> db.test
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 方式二</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> collection = db['test']</span></pre>
</div>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">5、插入数据</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">插入一条数据,MongoDB 每条记录都有一个唯一标识。返回一个 InsertOneResult 对象,若需要获取唯一标识,找到 InsertOneResult 对象的属性 inserted_id 即可</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> pymongo <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient

</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> mongodb:
    </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span>(self,host,db,port = 27017<span style="color: rgba(0, 0, 0, 1)">):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param host: str mongodb地址
      :param db: str 数据库
      :param port: int 端口,默认为27017
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      host </span>=<span style="color: rgba(0, 0, 0, 1)"> host
      db </span>=<span style="color: rgba(0, 0, 0, 1)"> db
      self.port </span>=<span style="color: rgba(0, 0, 0, 1)"> port
      client </span>= MongoClient(host=host,port=<span style="color: rgba(0, 0, 0, 1)">port)
      self.db </span>=<span style="color: rgba(0, 0, 0, 1)"> client

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> insert_one(self,table,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 要插入的字典
      :return: 返回一个包含ObjectId类型的对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> collection.insert_one(dic)

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep<br></span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
    dic </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓名</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小明</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">100</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">math</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">90</span><span style="color: rgba(0, 0, 0, 1)">}
    db </span>= mongodb(host=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>,db = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    rep </span>= db.insert_one(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,dic)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(rep.inserted_id)</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">插入多条数据,使用 insert_many 批量插入</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> pymongo <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient

</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> mongodb:
    </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span>(self,host,db,port = 27017<span style="color: rgba(0, 0, 0, 1)">):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param host: str mongodb地址
      :param db: str 数据库
      :param port: int 端口,默认为27017
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      host </span>=<span style="color: rgba(0, 0, 0, 1)"> host
      db </span>=<span style="color: rgba(0, 0, 0, 1)"> db
      self.port </span>=<span style="color: rgba(0, 0, 0, 1)"> port
      client </span>= MongoClient(host=host,port=<span style="color: rgba(0, 0, 0, 1)">port)
      self.db </span>=<span style="color: rgba(0, 0, 0, 1)"> client

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> insert_one(self,table,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 要插入的字典
      :return: 返回包含一个ObjectId类型的对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> collection.insert_one(dic)

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> insert_many(self,table,lists):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 要插入的列表,列表中的元素为字典
      :return: 返回包含多个ObjectId类型的列表对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> collection.insert_many(lists)

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep


</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
    lists </span>= [{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓名</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小明</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">100</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">math</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">90</span><span style="color: rgba(0, 0, 0, 1)">},
             {</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓名</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">小华</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">90</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">math</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">}]
    db </span>= mongodb(host=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>,db = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    rep </span>= db.insert_many(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,lists)
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> i <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> rep.inserted_ids:
      </span><span style="color: rgba(0, 0, 255, 1)">print</span>(i)</pre>
</div>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">6、查询</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">1)常规查询</span></p>
<ul>
<li><span style="font-family: &quot;comic sans ms&quot;, sans-serif">&nbsp;find_one :查询单条记录,返回一个字典。</span></li>
<li><span style="font-family: &quot;comic sans ms&quot;, sans-serif">&nbsp;find:查询多条记录 ,返回一个游标对象。</span></li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> pymongo <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient

</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> mongodb:
    </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span>(self,host,db,port = 27017<span style="color: rgba(0, 0, 0, 1)">):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param host: str mongodb地址
      :param db: str 数据库
      :param port: int 端口,默认为27017
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      host </span>=<span style="color: rgba(0, 0, 0, 1)"> host
      db </span>=<span style="color: rgba(0, 0, 0, 1)"> db
      self.port </span>=<span style="color: rgba(0, 0, 0, 1)"> port
      client </span>= MongoClient(host=host,port=<span style="color: rgba(0, 0, 0, 1)">port)
      self.db </span>=<span style="color: rgba(0, 0, 0, 1)"> client

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> find_one(self,table,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 查询条件
      :return: dict 返回单条记录的字典
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> collection.find_one(dic)

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> find(self,table,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 查询条件
      :return: list 返回查询到记录的列表
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> list(collection.find(dic))

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep

</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 查询 English 成绩为 100 的所有记录</span>
    dic = {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">}
    db </span>= mongodb(host=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>,db = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    rep </span>= db.find(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,dic)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span>(rep)</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">2)范围查询</span></p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">有时候我们需要范围比较查询,比如要查询 English 成绩为 80~90 ,可以使用比较符:dic = {'English':{'$in':}}</span></p>
<ul>
<li>$lt :小于</li>
<li>$lte:小于等于</li>
<li>$gt:大于</li>
<li>$gte:大于等于</li>
<li>$ne:不等于</li>
<li>$in:在范围内</li>
<li>$nin:不在范围内</li>
</ul>
<p>&nbsp;</p>
<p>3)逻辑查询</p>
<p>逻辑操作符用于连接多个条件查询,包括And运算($and)、Or运算($or)和Not运算($not)等</p>
<pre class="language-python highlighter-hljs"><code># 查询姓名是“小明”“小华”的数据
dic = {"$or":[{"姓名":"小明"},{"姓名":"小华"}]}
rep = list(collection.find(dic))</code></pre>
<p>&nbsp;</p>
<p>3)模糊查询</p>
<p>如果想要类似这种 sql 语句查询: select * from db where name like '%abc%'。在 MongoDB 中可以通过正则表达式来实现</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 模糊搜索key为"姓名",value包含"明"的记录</span>
dic = {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓名</span><span style="color: rgba(128, 0, 0, 1)">'</span>:{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$regex</span><span style="color: rgba(128, 0, 0, 1)">'</span>:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">明</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">}}
rep </span>= list(collection.find(dic))</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">4)字段筛选</span></p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">如果只想返回指定的字段,使用 find(find_one)方法时加上 projection 参数。类似 sql 语法:select Englist from db where name like '%abc%'</span></p>
<div class="cnblogs_code" contenteditable="false">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> projection字典参数。1显示,0不显示</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 以下查询结果只返回key为English的相关字段</span>
rep = list(collection.find(dic,projection={<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:1,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">_id</span><span style="color: rgba(128, 0, 0, 1)">'</span>:0}))</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">5)计数</span></p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">直接调用 count() 方法,返回一个 int 类型的数字</span></p>
<div class="cnblogs_code" contenteditable="false">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 计数查询只需要在普通查询后加上 count() 即可</span>
count =<span style="color: rgba(0, 0, 0, 1)"> collection.find().count()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> count = collection.find({'English':{'$gt':90}}).count()</span></pre>
</div>
<p>新版本的pymongo把count()弃用了。使用新方法estimated_document_count(),带条件的计数使用count_documents()</p>
<pre class="language-python highlighter-hljs"><code># 查询总数
count = collection.estimated_document_count()
# 按条件查询
count = collection.count_documents({"Englist":100})</code></pre>
<p>&nbsp;</p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">6)排序</span></p>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">排序时,直接调用sort()方法,并在其中传入排序的字段及升降序标志,返回一个游标对象</span></p>
<div class="cnblogs_code" contenteditable="false">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 正序 ASCENDING,倒序 DESCENDING。list()将游标对象转成列表</span>
data = list(collection.find(dic).sort(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">姓名</span><span style="color: rgba(128, 0, 0, 1)">'</span>,pymongo.DESCENDING))</pre>
</div>
<p>&nbsp;</p>
<p>7)返回指定条数记录</p>
<p>对查询结果设置指定条数的记录可以使用 limit() 方法,该方法只接受一个数字参数</p>
<pre class="language-python highlighter-hljs"><code># 返回前10条记录
data = list(collection.find(dic).limit(10))</code></pre>
<p>&nbsp;</p>
<p>8)指定从哪条数据开始查询</p>
<p>对查询结果设置指定开始位置查询可以使用 skip() 方法,该方法只接受一个数字参数</p>
<pre class="language-python highlighter-hljs"><code># 从第五条记录开始,返回10条记录
data = list(collection.find(dic).skip(5).limit(10))</code></pre>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">7、更新数据</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">首选查到需要更新的数据,然后将该数据更新,返回一个 UpdataResult 对象, raw_result 属性中包含 update 生效的个数。</span></p>
<ul>
<li><span style="font-family: &quot;comic sans ms&quot;, sans-serif">update_one:更新查询到的第一条数据</span></li>
<li><span style="font-family: &quot;comic sans ms&quot;, sans-serif">update_many:更新多条数据</span></li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> pymongo <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient

</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> mongodb:
    </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span>(self,host,db,port = 27017<span style="color: rgba(0, 0, 0, 1)">):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param host: str mongodb地址
      :param db: str 数据库
      :param port: int 端口,默认为27017
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      host </span>=<span style="color: rgba(0, 0, 0, 1)"> host
      db </span>=<span style="color: rgba(0, 0, 0, 1)"> db
      self.port </span>=<span style="color: rgba(0, 0, 0, 1)"> port
      client </span>= MongoClient(host=host,port=<span style="color: rgba(0, 0, 0, 1)">port)
      self.db </span>=<span style="color: rgba(0, 0, 0, 1)"> client

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> update_one(self,table,condition,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param condition: dict 查询条件
      :param dic: dict 更新的数据
      :return: 返回UpdateResult对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> $set 表示只更新dic字典内存在的字段</span>
      rep = collection.update_one(condition,{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$set</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:dic})
      </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 会把之前的数据全部用dic字典替换,如果原本存在其他字段,则会被删除</span>
      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> rep = collection.update_one(condition, dic)</span>

      <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> update_many(self,table,condition,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param condition: dict 查询条件
      :param dic: dict 更新的数据
      :return:返回UpdateResult对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> $set 表示只更新dic字典内存在的字段</span>
      rep = collection.update_many(condition,{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">$set</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:dic})
      </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 会把之前的数据全部用dic字典替换,如果原本存在其他字段,则会被删除</span>
      <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> rep = collection.update_many(condition, dic)</span>

      <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep


</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
    condition </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:80<span style="color: rgba(0, 0, 0, 1)">}
    dic </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:60<span style="color: rgba(0, 0, 0, 1)">}
    db </span>= mongodb(host=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">mongodb-monitor.monitor.svc.test.local</span><span style="color: rgba(128, 0, 0, 1)">'</span>,db = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    rep </span>= db.update_one(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,condition,dic)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(rep.raw_result)<br>
    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出 {'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}</span></pre>
</div>
<p>&nbsp;</p>
<h4><span style="font-family: &quot;comic sans ms&quot;, sans-serif">8、删除</span></h4>
<p><span style="font-family: &quot;comic sans ms&quot;, sans-serif">删除和 update 类似,删除数据后,返回一个 DeleteResult 对象,&nbsp;raw_result 属性中包含 delete 的个数</span></p>
<ul>
<li><span style="font-family: &quot;comic sans ms&quot;, sans-serif">delete_one:删除查询到的第一条数据</span></li>
<li><span style="font-family: &quot;comic sans ms&quot;, sans-serif">delete_many:批量删除符合查询条件的数据</span></li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> pymongo <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient

</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> mongodb:
    </span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span>(self,host,db,port = 27017<span style="color: rgba(0, 0, 0, 1)">):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param host: str mongodb地址
      :param db: str 数据库
      :param port: int 端口,默认为27017
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      host </span>=<span style="color: rgba(0, 0, 0, 1)"> host
      db </span>=<span style="color: rgba(0, 0, 0, 1)"> db
      self.port </span>=<span style="color: rgba(0, 0, 0, 1)"> port
      client </span>= MongoClient(host=host,port=<span style="color: rgba(0, 0, 0, 1)">port)
      self.db </span>=<span style="color: rgba(0, 0, 0, 1)"> client

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> delete_one(self,table,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 查询条件
      :return: 返回DeleteResult对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> collection.delete_one(dic)

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep

    </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> delete_many(self,table,dic):
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
      :param table: str 数据库中的集合
      :param dic: dict 查询条件
      :return: 返回DeleteResult对象
      </span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
      collection </span>=<span style="color: rgba(0, 0, 0, 1)"> self.db
      rep </span>=<span style="color: rgba(0, 0, 0, 1)"> collection.delete_many(dic)

      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> rep


</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span>==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
    dic </span>= {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">English</span><span style="color: rgba(128, 0, 0, 1)">'</span>:60<span style="color: rgba(0, 0, 0, 1)">}
    db </span>= mongodb(host=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">localhost</span><span style="color: rgba(128, 0, 0, 1)">'</span>,db = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    rep </span>= db.delete_many(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,dic)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(rep.raw_result)
    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出 {'n': 21, 'ok': 1.0}</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/shenh/p/14416111.html
頁: [1]
查看完整版本: python 操作MongoDB详解