def mongo_input_data(client): # get a db db = client['test1'] # get a collection col = db['col1'] print col print db # item1 str0 = 'this is the first item. the world is a wonderful place.' str0 += 'it is amazing to be in this knowledge.' item0 = { 'name' : 'ITEM0', 'tdata' : str0 } col.insert_one(item0) rs = RandomSentence() # insert 20 elements for i in range(1,20): sent = rs.random_sentence() item = { 'name' : 'ITEM' + str(i), 'tdata' : sent } col.insert_one(item) print db.collection_names(include_system_collections=False) i = 0 p_dict = {} for item in col.find(): p_dict[i] = item i += 1 print_table_from_dict(['Key','Value'],p_dict) # create a text index # col.create_index({"tdata" : "text"}) col.ensure_index([('tdata' , 'text')], name='test_text_index') search_term = { "$text" : { "$search" : "Superman" } } x = col.find(search_term).limit(10) for item in x : print item
def handle_knowledge_save(self): # Make a UUID using the following INPUTs # # Make a uuid_seed_string = str(date) + str(series) # Use the uuid_seed_string with uuid.NAMESPACE_DNS + string (see uuid5()) # try: # create a UUID and store it into the dictionary uuid_seed_string = self.req_params['d_series'] + self.req_params['d_date'] + self.req_params['d_sheetno'] cur_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, uuid_seed_string) debug_print(5, "cur_uuid: ", cur_uuid, uuid.NAMESPACE_DNS) self.req_params['d_uuid'] = str(cur_uuid) if global_args.debug_level >= 5: print_table_from_dict([ 'POST param', 'Value'], self.req_params) # make a file name # The filename is "SERIES_<series_string>_DATE_<date_string>_SEQ_<sheet_no>.json" file_name = '' file_name += 'SERIES_' file_name += self.req_params['d_series'].replace(' ','_') file_name += '_DATE_' file_name += self.req_params['d_date'].replace('/','_').replace('-','_') file_name += '_SEQ_' file_name += self.req_params['d_sheetno'] file_name += ".json" debug_print(5, "JSON file_name: ", file_name) # now write to a file full_file_name = global_args.base_dir + 'data/files/' + file_name with open(full_file_name, 'w') as outfile: json.dump(self.req_params, outfile, sort_keys = True, indent = 4, ensure_ascii=False) return "<p> HEE HEE: Your login information was correct.</p>" except: print_exception(True)