def select_many(self, sha256_digest=None, scale=None, command=None, args=None, order=pymongo.DESCENDING, sort=None): """Select commands. Args: sha256_digest (str, optional): The hash of the file. Defaults to None. scale (str, optional): The scale. Defaults to None. command (str, optional): The command. Defaults to None. args (dict, optional): The arguments. Defaults to None. callback (func, optional): The callback function. Defaults to None. Returns: :obj:`Cursor`: The mongodb cursor. """ data = { "sha256_digest": sha256_digest, "scale": scale, "command": command, "args": args } keys = [k for k, v in data.items() if v is None] for k in keys: del data[k] cursor = self.db.commands.find(data) if sort: cursor = cursor.sort([ (sort, order) ]).collation(collation.Collation(locale="en")) return cursor
def _try_creating_indexes(self): """ Tries to create indexes (if they have not already been created) that optimize the performance of this application. """ post_indexes = self.posts.list_indexes() tag_indexes = self.tags.list_indexes() vote_indexes = self.votes.list_indexes() print('Creating indexes...') if self.question_search_index not in post_indexes: self.posts.create_index([('Tags', TEXT), ('Title', TEXT), ('Body', TEXT)], default_language='none', name=self.question_search_index) if self.find_answers_index not in post_indexes: self.posts.create_index([('PostTypeId', ASCENDING), ('ParentId', ASCENDING)], name=self.find_answers_index) if self.posttypeid_index not in post_indexes: self.posts.create_index([('PostTypeId', ASCENDING)], name=self.posttypeid_index) if self.post_Id_index not in post_indexes: self.posts.create_index([('Id', ASCENDING)], collation=collation.Collation( 'en_US', numericOrdering=True), name=self.post_Id_index) if self.post_owner_index not in post_indexes: self.posts.create_index([('PostTypeId', ASCENDING), ('OwnerUserId', ASCENDING)], name=self.post_owner_index) if self.tag_Id_index not in tag_indexes: self.tags.create_index([('Id', ASCENDING)], collation=collation.Collation( 'en_US', numericOrdering=True), name=self.tag_Id_index) if self.vote_Id_index not in vote_indexes: self.votes.create_index([('Id', ASCENDING)], collation=collation.Collation( 'en_US', numericOrdering=True), name=self.vote_Id_index) if self.vote_userid_index not in vote_indexes: self.votes.create_index([('UserId', ASCENDING)], name=self.vote_userid_index) if self.vote_postid_userid_index not in vote_indexes: self.votes.create_index([('PostId', ASCENDING), ('UserId', ASCENDING)], name=self.vote_postid_userid_index)
def get_id_for_collection(collection): numeric_collation = collation.Collation(locale="en_US", numericOrdering=True) ids = list( collection.find({}, { "id": 1, "_id": 0 }, sort=[("id", DESCENDING)], limit=1, collation=numeric_collation)) return 0 if not ids else int(ids[0]["id"])
def _get_new_id(self, id_type): """ Gets a new unique Id value either the Posts, Votes, or Tags collection as specified by id_type. This is done by finding the current max Id value in the respective collection and then incrementing that by 1 and returning the new value as a string. :param id_type: string that corresponds to the collection to get a new unique Id value for (one of 'post', 'vote', or 'tag') :return: a new unique Id value for the specified table """ res = {} max_id_query = [('Id', DESCENDING)] if id_type == 'post': res = self.posts.find_one(sort=max_id_query, collation=collation.Collation( 'en_US', numericOrdering=True)) elif id_type == 'vote': res = self.votes.find_one(sort=max_id_query, collation=collation.Collation( 'en_US', numericOrdering=True)) elif id_type == 'tag': res = self.tags.find_one(sort=max_id_query, collation=collation.Collation( 'en_US', numericOrdering=True)) return str(int(res['Id']) + 1)
def select_all(self, filter_=None, order=pymongo.DESCENDING, sort=None): """Select all commands. Returns: :obj:`Cursor`: The mongodb cursor. """ if filter_: documents = self.db.commands.find(filter_) else: documents = self.db.commands.find() if sort: documents = documents.sort([ (sort, order) ]).collation(collation.Collation(locale="en")) return documents
def select_all(self, filter_=None, order=pymongo.DESCENDING, sort=None, limit=0, skip=0): """Select all files. Args: filter_ (dict): The filter. Defaults to None. order (:obj:`int`, optional): Sort order. Defaults to DESCENDING. sort (dict): Sorting parameters. Defaults to None. Returns: :obj:`Cursor`: The mongodb cursor. """ documents = [] if filter_: documents = self.db.files.find(filter_, limit=limit, skip=skip) else: documents = self.db.files.find(limit=limit, skip=skip) if sort: documents = documents.sort([(sort, order)]).collation(collation.Collation(locale="en")) return documents
import discord """ More of a playbook to init collections..shouldn't have to do this but just in case. """ def connections(): mongoclient = pymongo.MongoClient( "redacted for security conecerns TODO add to env on startup") discordclient = discord.Client() return discordclient, mongoclient if __name__ == "__main__": client, garbage = connections() cocodb = garbage.cocodb #cocodb.create_collection('fish', collation=collation.Collation(locale='en_US', strength = 2)) cocodb.create_collection('bugs', collation=collation.Collation(locale='en_US', strength=2)) #cocodb.create_collection('villagers', collation=Collation(locale='en_US', strength = 2)) cocodb.fish.create_index([("name", pymongo.ASCENDING), ("hemisphere", pymongo.ASCENDING)], unique=True) cocodb.bugs.create_index([("name", pymongo.ASCENDING), ("hemisphere", pymongo.ASCENDING)], unique=True)