def mongo_request_sorted(Dataset_Name, MongoDBClient, DB_Name, DB_collectionName, findDict, returnFields, sortField, sortDirection, perPage, pageN): """ Запрос к базе данных mongoDB :return: строка с ответом из базы данных в формате JSON """ if not returnFields: returnFields = {u"FakeField": 0} if len(returnFields) == 0: returnFields[u"FakeField"] = 0 if pageN > 0: pageN -= 1 MNGdb = MongoDBClient[DB_Name] MNGcoll = MNGdb[DB_collectionName] dataCursor = MNGcoll.find(findDict, returnFields).sort(sortField, direction=sortDirection).limit(perPage).skip( perPage * pageN) total = dataCursor.count() totalOnPage = dataCursor.count(True) MongoDBClient.disconnect() if totalOnPage > 0: # TODO: заглушка на json dataStr = preparation_response(dataCursor, Dataset_Name, total, perPage, pageN, "json") else: dataStr = None return dataStr
def mongo_requestIN(dataset_name, mongo_db_client, db_name, db_collection_name, id_list, find_dict, return_fields, per_page, page_num, sort_field=None, sort_direction=None, sort_by_revelance=False, search_data=None, format='json', report=False): """ Запрос к базе данных mongoDB (поиск документов только :return: строка с ответом из базы данных в формате JSON """ if not return_fields: return_fields = {u"FakeField": 0} if len(return_fields) == 0: return_fields[u"FakeField"] = 0 if page_num > 0: page_num -= 1 MNGdb = mongo_db_client[db_name] MNGcoll = MNGdb[db_collection_name] if sort_by_revelance: search_id_list = list() docList = list() for doc_id, search_rank in search_data.iteritems(): docList.append({u"id": doc_id, u"searchRank": search_rank}) search_id_list = sorted(docList, key=lambda doc: doc[u"searchRank"])[per_page * page_num:per_page * (page_num + 1)] search_id_list = map(lambda doc: dbid(doc[u"id"]), search_id_list) find_dict[u"_id"] = {u"$in": search_id_list} else: find_dict[u"_id"] = {u"$in": id_list} if report: per_page = SPHNX_LIMIT_REPORT if sort_field == None or sort_direction == None: if sort_by_revelance: data_cursor = MNGcoll.find(find_dict, return_fields) else: data_cursor = MNGcoll.find(find_dict, return_fields).limit(per_page).skip(per_page * page_num) else: try: data_cursor = MNGcoll.find(find_dict, return_fields).sort(sort_field, direction=sort_direction).limit( per_page).skip(per_page * page_num) except: data_cursor = MNGcoll.find(find_dict, return_fields).limit(per_page).skip(per_page * page_num) if sort_by_revelance: if len(find_dict) == 1: total_on_page = len(search_id_list) total = len(id_list) else: total_on_page = data_cursor.count() find_dict[u"_id"] = {u"$in": id_list} total = MNGcoll.find(find_dict, return_fields).limit(per_page).skip(per_page * page_num).count() find_dict[u"_id"] = {u"$in": search_id_list} else: total = data_cursor.count() total_on_page = data_cursor.count(True) mongo_db_client.disconnect() if total_on_page > 0: # TODO: заглушка на json if report: data_str = get_report(data_cursor) elif sort_by_revelance: data_str = preparation_response(data_cursor, dataset_name, total, per_page, page_num, format, search_data) else: data_str = preparation_response(data_cursor, dataset_name, total, per_page, page_num, format) else: data_str = None return data_str