zoukankan      html  css  js  c++  java
  • python和mongodb简单交互

    python和mongodb简单交互

    1.安装pymongo:

    pip3 install pymongo
    

    2.pymongo的简单用法:

    # /usr/bin/env python3
    import pymongo
    
    
    class Mymongo(object):
        def __init__(self, host, port=27017):
            try:
                self.conn = pymongo.MongoClient(host=host, port=port)
            except Exception as e:
                print("连接失败", e)
            else:
                print('连接成功')
    
        def add(self, p_dict):
            res = self.conn.testdb.testcol.insert(p_dict)  # insert需要传入字典
            if res:
                print('插入成功')
    
        def show(self):
            res = self.conn.testdb.testcol.find()  # find()返回一个可迭代对象
            for i in res:
                print(i)
    
        def mod(self, before, after):   # 需要传入字典
            res = self.conn.testdb.testcol.update(before, after)
            if res:
                print("修改成功", res)
    
        def rem(self, p_dict):
            res = self.conn.testdb.testcol.delete_one(p_dict)
            if res:
                print('删除成功')
    
    if __name__ == "__main__":
        mymongo = Mymongo('192.168.26.135')
        mymongo.add({"name": 'Tom'})
        mymongo.show()
        mymongo.mod({"name": 'Tom'}, {"name": 'Jerry'})
        mymongo.show()
        mymongo.rem({"name": 'Jerry'})
        mymongo.show()
    
    

  • 相关阅读:
    CodeForces
    设计模式之装饰模式和代理模式区别与联系
    java反射 概念
    Java 反射详解 转载
    Spring--AOP 例子
    MD5加密
    面向对象编程思想(OOP)
    软件测试assert
    junit4.9测试用例 spring测试用例 Assert 注解
    断言
  • 原文地址:https://www.cnblogs.com/PrettyTom/p/6893579.html
Copyright © 2011-2022 走看看