PyMongo (MongoDB / Python) wildcard query issue.

46 views
Skip to first unread message

MikeAW2010

unread,
Feb 12, 2018, 7:04:03 AM2/12/18
to mongodb-user
I am attempting to write a python program that will allow me to query the results of what I may input. If for any reason I don't have the entire matching input then I would like to put an asterisk by the input and have Python display any matching or similar output. For example, if there was a State field, and I input "M*" then I should come up with MA, MO, and MI results...

What I am getting instead however is that Python is outputting the entire collection and I don't understand why... I am using the re.compile function for my wildcard. In the Python SHELL it works fine but in the script Im getting the entire collection.

Can you explain whats going on?

The Script:

## PyMongo database import and query tool is designed to import Documents into the "zips" collection of the database named "test" which is installed on bammbamm

## The script is currently designed only to be run locally or via SSH directly from bammbamm although remote execution should be just as possible.
## Currently I only know how to retrieve specific documents but later intend to add functionality to display all documents within a Collection simultaneously.

## Imports


import pymongo
import re
from pymongo import MongoClient
connection = MongoClient()

## Connection Definitions

db = connection.test
collection = db.zips

## Startup Variables & Definitions

search = 0 ####  Defines and enables the search variable.
query = 0 #### Defines and enables the query variable.
query_type = 0 #### Defines and enables the query variable.
start_counter = 0 #### Defines and enables the start counter, setting it to 0 when the program is launched, it will later be set to 1 so the start message will not unecessarily repeat itself.
def Hello_Message(): #### The startup message.
print ("Welcome to the MongoDB / Python debug tool. Remember to close this tool at any time, press Ctrl+C.")
print ("To search the MongoDB, First select a search method, and then type in your search parameters.")
print ("Search methods are as follows:")
def Instructions(): #### Prints the list of instructions.
print ("Use 'O' for (O)bject ID")
print ("Use 'P' for (P)opulation")
print ("Use 'L' for (L)ocation")
print ("Use 'C' for (C)ity")
print ("Use 'S' for (S)tate")
def Input(): #### Assigns the "search" variable to a search method - O for Object ID, N for Name, ect - this later tells the query tool by which means you will be searching.
global search
search = input("Please select your search method: ")
def Query(): 
global query
global query_type
if search == 'O':
query_type = '_id'
query = input("Object ID Search: ")
elif search == 'P':
query_type = 'pop'
query = int(input("Population Search: "))
elif search == 'L':
query_type = 'loc'
query = input("Location Search: ")
elif search == 'C':
query_type = 'city'
query = input("City Search: ")
elif search == 'S':
query_type = 'state'
query = input("State: ")
else: 
print ('Invalid search option, please try again.')
## Program

while start_counter == 0:
Hello_Message()
start_counter = 1
while start_counter == 1:
print (" ")
Instructions()
print (" ")
Input()
print (" ")
Query()
print (" ")
results = db.zips.find({query_type:re.compile(query)}) #### Results are compiled into a list defined by the query. This allows us to display multiple results based on multiple querries.
for result in results:
print (result)
print (" ")
print (" ")



Wan Bachtiar

unread,
Feb 13, 2018, 7:57:12 PM2/13/18
to mongodb-user

Can you explain whats going on?

Hi Mike,

The reason why you’re getting the entire collection is because of the regular expression you’re using in the filter.

When you specified M*, it means to match zero or more occurrences of letter ‘M’. It is similar as to specify M{0,}.
Thus the query returns all entry in the collection where state contains M or no M.

What you’re intending to specify is most likely ^M.*, which means to match a value beginning with ‘M’ AND also zero or more occurences of any characters (except line break) after 'M'. See also MongoDB Regex Index Use.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages