import pymongo


class Mongolib:
    def __init__(self, db, uri):
        self.conn = pymongo.MongoClient()
        self.conn = pymongo.MongoClient(uri)[db]

    def find(self, collection_name, query, limit=5, sort = None ,projection = None):
        collection = self.conn[collection_name]
        if sort is None:
            data = collection.find(query, projection).limit(limit) 
        else:
            data = collection.find(query, projection).sort(sort[0],sort[1]).limit(limit)
        return data 

    def update_many(self, collection_name, query, data):
        collection = self.conn[collection_name]
        data = collection.update_many(query, data)
        return data

    def insert_one(self, collection_name, data):
        collection = self.conn[collection_name]
        
        # db.test.insert_one({'x': 1})
        data = collection.insert_one(data)
        return data
