123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
- @AUTHOR: ASJRD018
- @NAME: Ansjer
- @software: PyCharm
- @DATE: 2018/6/15 14:18
- @Version: python3.6
- @MODIFY DECORD:ansjer dev
- @file: mongodb.py
- @Contact: chanjunkai@163.com
- """
- # !/usr/bin/python3
- import pymongo
- class mongodb(object):
- def __init__(self):
- myclient = pymongo.MongoClient('mongodb://localhost:27017/')
- # myclient = pymongo.MongoClient('mongodb://192.168.136.45:27017/')
- self.myclient = myclient
- self.db = myclient['ansjertest']
- def check_db_exist(self, database):
- dblist = self.myclient.database_names()
- if database in dblist:
- return True
- else:
- return False
- def insert_one(self, col, data):
- column = self.db[col]
- res = column.insert_one(data)
- return res.inserted_id
- def findAll(self, col,page,line,query):
- collist = self.db.collection_names()
- if col in collist: # 判断 customers 集合是否存在
- qs = self.db[col].find(query)
- count = qs.count()
- if page != 0 and line != 0:
- qs = qs.sort("_id", -1).skip((page - 1) * 10).limit(line)
- # print(count)
- data = []
- for q in qs:
- data.append(q)
- res_dict = {'data': data, 'count': count}
- return res_dict
- else:
- print("集合不存在!")
- return False
- if __name__ == '__main__':
- mdb = mongodb()
- col = "log_device_operation"
- data = {"name": "111", "address": "Lowstreet 27"}
- flag = mdb.insert_one(col=col, data=data)
- # qs = mdb.findAll(col=col, page=1, line=100,query={'name':'Peter'})
- # print(qs)
|