def submitDistinctQuery(dbh, p, collection, emoCollection, check, startsWith=None, wordLength=None, edits=None): '''Query mongo for a UNIQUE list of phonetics.''' # Quick db quthorisation catch try: collection.find_one() except (pymongo.errors.OperationFailure, pymongo.errors.AutoReconnect): mdb.authenticate(dbh, p.dbUser, p.dbPassword) # Make sure we use the right collection if check == 'emo': res = emoCollection.distinct(check) else: # Getting the distinct words/pho based on their length if wordLength and edits: wl, edits = int(wordLength), int(edits) field = "%slen" %(check) q = {field:{"$gte": wl-edits, "$lte": wl+edits}} print "WordlengthEdits:", q res = collection.find(q).distinct(check) # Only get back the distinct for this leading letter elif startsWith: q = {check : {'$regex':'^%s' %startsWith.lower()}} print "Regex Query:", q res = collection.find(q).distinct(check) else: res = collection.distinct(check) # Iterate the results into a list - done like this to remove None vals out = "" for token in res: if token: out += "%s###" %(token) out.rstrip('###') return out
def submitQuery(dbh, p, collection, emoCollection, token, flds, check, output, regex=None): '''Query mongo with query and optionally just count.''' # Quick db quthorisation catch try: collection.find_one() except (pymongo.errors.OperationFailure, pymongo.errors.AutoReconnect): mdb.authenticate(dbh, p.dbUser, p.dbPassword) if check == 'emo': results = checkEmo(emoCollection, token) if output == 'exists': results = checkLengthForExists(results) elif output == 'all': if len(results) == 0: results = [] else: out = [] for document in results: out.append({output:document[output]}) results = out # Check against every field to see if it exists in it elif check == 'all': results = checkAll(collection, flds, token.lower()) if output == 'exists': results = checkLengthForExists(results) # Return complete documents elif output == 'all': if len(results) == 0: results = [] # Check all fields, output just 'output' selection else: out = [] for document in results: out.append({output:document[output]}) results = out # Query the specific field specified in 'check' else: results = [] # Allow for regex query if regex == True: query = {check : {'$regex':'.*%s.*' %token.lower()}} else: query = {check : token.lower()} res = collection.find(query) for r in res: del r['_id'] results.append(r) # Just return whether it exists if output == 'exists': results = checkLengthForExists(results) # Return complete documents elif output == 'all': if len(results) == 0: results = {} # Output is either pho, word or slang, so just extract that field else: out = [] for document in results: newRes = {output:document[output]} out.append(newRes) results = out return json.dumps(results)