• python操作MongoDB数据库


    python操作MongoDB数据库需要用到pymongo的库。

    pip install pymongo

    使用时需要先导入库。

    import pymongo

    建立连接

    import pymongo
    client =pymongo.MongoClient(host="localhost",port=27017)

    指定数据库

    db=client['joe']

    指定集合

    collection=db['age']

    操作

    查找文档

    由于查找到的文档是一个迭代的对象,所以可用__next__()方法一个一个的取,同样也可以用for循环来操作,这里采用第二种。

    documents=collection.find()
    for data in documents:
        print(data)

    增加文档

    这里增加的文档要严格以类json的数据格式,以键值对的形式添加数据。

    collection.insert({'hellon':30})            #添加一条
    collection.insert([{"one":12},{"two":13}]) #添加多条

    删除文档

    collection.remove({"one":12})                #删除一条
    collection.remove({"hellon":30},{"two":13}) #删除多个数据,注意若库中存在两条{"hello":30}的数据会都删掉
    collection.remove({"one":12},multi=False)   #multi参数指定是否要删除多条数据,False代表删除一条

    修改文档

    collection.update({"one":12},{"name":"joe"})             #更新所有数据
    collection.update({"one":12},{"name":"joe"},multi=False)#更新一条数据,multi含义代表是否更新多条

    以上的方法在python3中官方已经不推荐使用,但是现在也是可已使用的,取而代之的是下面的方法,用法与上面的没有太大差别。

    查找

    find_ong() #查找一条文档
    find_many()#查找多条文档

    增加

    insert_one()#插入一条文档
    insert_many()#插入多条文档

    删除

    delete_one()#删除一条文档
    delete_many()#删除多条文档

    修改

    update_one()#更新一条文档
    update_many()#更新多条文档

    综合举例

    # -*- coding: utf-8 -*-
    # @Time    : 2018/9/8 10:27
    # @Author  : Joe
    # @Email   : 18842114496@163.com
    # @File    : 20180908.py
    # @Software: PyCharm
    # 将MongoDB的find, insert, update, remove方法封装成类
    import pymongo
    class MongoDB:
        def __init__(self,db_name):
            self.client=pymongo.MongoClient()
            self.db=self.client[db_name]
        def find(self,collection_name):
            document_data=self.db[collection_name].find()
            for data in document_data:
                print(data)
        def insert(self,collectionn_ame,documents,insert_many=False):
            if insert_many==False:
                self.db[collectionn_ame].insert_one(documents)
            else:
                self.db[collectionn_ame].insert_many(documents)
        def update(self,collection_name,documents_updated,document_update):
            self.db[collection_name].update(documents_updated,document_update)
        def remove(self,collection_name,delete_data,delete_many=False):
            if delete_many==False:
                self.db[collection_name].delete_one(delete_data)
            else:
                self.db[collection_name].delete_many(delete_data)
    if __name__=='__main__':
        mm=MongoDB('joe')
        mm.find('age')
        one={"one":"Jack"}
        many=[{"two":"Alex"},{"three":"Jerray"},{"four":"Helln"}]
        mm.insert('name',one,insert_many=False)
        mm.insert('name',many,insert_many=True)
        up1={"three":"Jerray"}
        up2={"age":20}
        mm.update('name',up1,up2)
        delete={"two":"Alex"}
        mm.remove('name',delete,delete_many=True)
  • 相关阅读:
    [Cocos2d-x]Cocos2d-x开发中C++调用Object-c
    [数据结构]基本概念
    [Cocos2d-x]Mac下运行HelloCpp For Android
    [Android] JDK , NDK , JNI
    [Cocos2d-x]坐标系
    [Android]mac下开发环境搭建
    [Cocos2d-x]博客推荐
    nyoj-506-洗澡
    nyoj-479-Coprimes
    nyoj-464-Cookies
  • 原文地址:https://www.cnblogs.com/austinjoe/p/9615317.html
Copyright © 2020-2023  润新知