Python Forum
pymongo wildcard query issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pymongo wildcard query issue
#1
I am using the re module to interpret wildcards in my input functions and I am attempting to write a Python Script that will look in the database named "test" and the collection named "zips" and pull data based on what I input. So first it will ask what kind of search I want and then it will ask the criteria. The criteria should be flexible so if there is a wildcard, I need it to be able to pull any similar or matching criteria. For example if I did a "State" search and the criteria I input was M* - I would want MD, MA, MO, MI, and MS results... What I am getting instead is if I use a wildcard, Python / MongoDB is literally spilling out the contents of the ENTIRE collection, and not just the filtered results of MD, MA, MO, MI.. ect.. happens on any kind of search as well. If I do just a specific search lets say "MO" then I only get MO results but if I do M* then I get things that start with A, B, C ect..and many that don't even have M in it at all..its spilling the entire collection.

Here is my code: What can I do to fix this?

## 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 (" ")
Reply
#2
Type out your problem like I type this sentence.
Press enter.
Provide information about the states like this:
states = [my, nice, python, code]

And honor the same biological/ergonomic concession
in your programming code
by not letting everything run the **** off of the screen.

I've re-formatted your code for you, closer to PEP8.
Now, please, google "how to wildcard search in pymongo" and leave this webpage alone.

import pymongo
import re
from pymongo import MongoClient

# Connection
connection = MongoClient() 
db = connection.test
collection = db.zips
 
# Constants are usually CAPITALIZED but it's all good.
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.")
    print ("Remember to close this tool at any time, press Ctrl+C.")
    print ("To search the MongoDB, First select a search method,")
    print (" 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 informs the query tool of your search criteria
    """
    global search
    search = input("Please select your search method: ")


def Query():
    """ Run the 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.')


if __name__ == "__main__":
    """ Main Execution body """
    while start_counter == 0:
        Hello_Message()
        start_counter = 1
    while start_counter == 1:
        Instructions()
        Input()
        Query()
        # Results are compiled into a list defined by the query. 
        # This allows us to display multiple results based on multiple queries.
        results = db.zips.find({query_type:re.compile(query)}) 
        for result in results:
            print (result)
        print()
Reply
#3
(Apr-09-2019, 06:32 PM)reniformpuls Wrote: This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this This is what your question looks like no one wants to read this.

How do I fix this? ^



Type out your problem like I type this sentence.
Press enter.
Provide information about the states like this:
states = [my, nice, python, code]

And honor the same biological/ergonomic concession
in your programming code
by not letting everything run the **** off of the screen.

I've re-formatted your code for you, closer to PEP8.
Now, please, google "how to wildcard search in pymongo" and leave this webpage alone.

import pymongo
import re
from pymongo import MongoClient

# Connection
connection = MongoClient() 
db = connection.test
collection = db.zips
 
# Constants are usually CAPITALIZED but it's all good.
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.")
    print ("Remember to close this tool at any time, press Ctrl+C.")
    print ("To search the MongoDB, First select a search method,")
    print (" 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 informs the query tool of your search criteria
    """
    global search
    search = input("Please select your search method: ")


def Query():
    """ Run the 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.')


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

Google and leave this webpage alone? Are you absolutely retarded? You are the epitome of the loser who thinks he's a hotshot because you got one answer +5'd on Stack Overflow. Your entire answer was quite hilariously absolutely useless, which I imagine the rest of your pathetic, inbred life must be. I hope you make some friends soon and stop being miserable on online forums where newbies are trying to learn to soothe your rather pathetic ego.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  websockets and pymongo exception sankar2000 0 1,010 Oct-07-2022, 09:52 PM
Last Post: sankar2000
  PyMongo error bagou450 0 809 Aug-04-2022, 08:27 AM
Last Post: bagou450
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 7,118 Jan-13-2022, 10:20 PM
Last Post: randolphoralph
Exclamation MongoDB cannot connect - pymongo speedev 1 2,094 Aug-21-2021, 01:35 PM
Last Post: ndc85430
  Escaping '$' in pymongo raulp2301 0 1,644 Feb-14-2020, 03:48 PM
Last Post: raulp2301
  Load JSON file data into mongodb using pymongo klllmmm 1 11,869 Jun-28-2019, 12:47 AM
Last Post: klllmmm
  python script to get wildcard mask output in the following format techrichit 0 3,829 Aug-10-2018, 11:01 PM
Last Post: techrichit
  Finding directory based on wildcard? jkimrey 4 3,399 Apr-25-2018, 10:02 AM
Last Post: mlieqo
  sqlite3 wildcard use problems lusticus 1 3,094 Apr-11-2018, 08:22 PM
Last Post: woooee
  how to get the list of databases to a variable using pymongo? dvsrk563 1 10,730 Aug-10-2017, 08:01 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020