Python与MogoDB交互

睡了大半天,终于有时间整理下拖欠的MongoDB的封装啦。

首先我们先进行下数据库的连接:

        conn = MongoClient('localhost',27017)   # 建立连接

        result = conn.list_database_names() # 查看mongodb里面有哪些数据库
        print(result)

        db = conn['test'] # 选择数据库,当你选择的不存在的时候,会自动帮你创建
        print(db.collection_names()) # 查看当前数据库里面有哪些集合

        self.collection = db['table']  # 选择集合,当集合不存在也会自动创建

那么这个时间, 我简单操作下:

ps:本段代码为废话,可直接略过。test调试

data = {"a":1, "b":2}
# collection.insert(data)
collection.insert_one(data)

# collection.insert([{"a":1, "b":2}, {"a":1, "b":2}])
collection.insert_many([{"a":1, "b":2}, {"a":1, "b":2}])

# result = collection.find_one()  # 查看一条数据
# key = collection.find_many()  # 查看一条数据


result = collection.find()  # 查看所有

for data in result:
    print(data)

# collection.remove()  # 删除所有
collection.delete_one({"a":1})
result = collection.find()  # 查看所有

for data in result:
    print(data)

 

这里进入正题吧。

先看下,mongoDB的方法都有哪些:

 

 详细的封装代码如下:

from pymongo import MongoClient
import pymongo
'''
查找一条文档: find_one()
查找所有:find()
添加一条文档:insert_one
添加多条:insert_many()
删除一条:delete_one()
删除多条:delete_many()
修改一条文档: update_one()
修改多条: update_many()
'''

class MongoDB():

    def __init__(self):

        conn = MongoClient('localhost',27017)   # 建立连接

        result = conn.list_database_names() # 查看mongodb里面有哪些数据库
        print(result)

        db = conn['test'] # 选择数据库,当你选择的不存在的时候,会自动帮你创建
        print(db.collection_names()) # 查看当前数据库里面有哪些集合

        self.collection = db['table']  # 选择集合,当集合不存在也会自动创建

    def insertDB(self, data):
        """直接使用insert() 可以插入一条和插入多条 不推荐 明确区分比较好"""
        return self.collection.insert_many(data) if len(data) > 1 else self.collection.insert_one(data)

    def findDB(self):   # 查询全部
        res = self.collection.find()
        for data in res:
            print(data)
    def find_oneDB(self, data):  # 单条查询
        res = self.collection.find_one(data)
        print(res)

    def removeDB(self):  # 删除全部
        self.collection.remove()

    def deleteDB(self, data):  # 删除单条
        self.collection.delete_one(data)

    def deleteManyDB(self):  # 删除多条
        self.collection.delete_many()

    def updateOneDB(self, fifler, updata):  # 单条编辑
        res = self.collection.update_one(fifler, updata)
        return res
    def updateManyDB(self, fifler, updata): # 批量编辑
        res = self.collection.update_many(fifler, updata)
        return res


if __name__ == '__main__':
    conn = MongoDB()
    
    # 以下为代码调试,我总得调试完成吧,要不会被骂的。
    # data = {"name":"456"}
    conn.insertDB([{"a":6, "b":6}, {"a":7, "b":7}])
    # conn.removeDB({"name":'456'})
    conn.deleteDB({"a":6, "b":6})
    # conn.updateOneDB({"a":7}, {"b":9})
    # conn.updateOneDB({"a":7}, {"$set":{"b":9}})
    conn.updateManyDB({"a":7}, {"$set":{"b":9}})
    conn.findDB()

 

到这关于mongoDB的知识,整理完毕。

另外关于updata参数报错:ValueError: update only works with $ operators

解决办法:

conn.updateManyDB({"a":7}, {"$set":{"b":9}})

 

 

作者:含笑半步颠√

博客链接:https://www.cnblogs.com/lixy-88428977

声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。

 

posted @ 2018-09-08 22:24  含笑半步颠√  阅读(445)  评论(0编辑  收藏  举报