Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

Python - MongoDB TypeError: find() takes at least 1 argument (0 given)

I'm searching through a MongoDB database to find a collection and pass it to another function that prints it to a file.

The name of my collection is: ec2_list_01122021

When I pass that collection to mongo db from a function called print_collections(), it complains that the find() command takes at least 1 argument:

Traceback (most recent call last):
  File "C:Usersdun0002OneDrive - Synchronoss TechnologiesDesktopimportant_foldersJokefiregitjf_cloud_scriptsaws_scriptspythonaws_toolsec2_mongo.py", line 533, in <module>
    main()
  File "C:Usersdun0002OneDrive - Synchronoss TechnologiesDesktopimportant_foldersJokefiregitjf_cloud_scriptsaws_scriptspythonaws_toolsec2_mongo.py", line 522, in main
    print_reports(interactive,aws_account,aws_account_number)
  File "C:Usersdun0002OneDrive - Synchronoss TechnologiesDesktopimportant_foldersJokefiregitjf_cloud_scriptsaws_scriptspythonaws_toolsec2_mongo.py", line 432, in print_reports
    mongo_export_to_file(interactive, aws_account, aws_account_number,instance_col)
  File "C:Usersdun0002OneDrive - Synchronoss TechnologiesDesktopimportant_foldersJokefiregitjf_cloud_scriptsaws_scriptspythonaws_toolsec2_mongo.py", line 279, in mongo_export_to_file
    mongo_docs = instance_col.find()
TypeError: find() takes at least 1 argument (0 given)

This is my code for print_collections():

def print_collections():
    myclient = connect_db()
    message = f"* Print DB Collections *"
    banner(message, border="*")
    print(f"This command prints the database collection names.
")
    if myclient != None:
        # the list_database_names() method returns a list of strings
        database_names = myclient.list_database_names()
        print ("There are", len(database_names), "databases.")
        for db_num, db in enumerate(database_names):
            print ("
Getting collections for database:", db, "--", db_num)
            collection_names = myclient[db].list_collection_names()
            print ("The MongoDB database returned", len(collection_names), "collections.")
            # iterate over the list of collection names
            for col_num, col in enumerate(collection_names):
                print (col, "--", col_num)

In my output I printed the value of the variable holding the collection name, so i don't know why the find command isn't working:

Enter the date in format 'dd/mm/yyyy': 01/12/2021
Input date is valid: 01/12/2021
Instance Collection: ec2_list_01122021 # <--- the collection that I'm passing to MongoDB
Interactive: 0

If I call the mongo_export_to_file() function directly from within this script and others, it does work. This is my mongo_export_to_file() function:

def set_db():
    myclient = connect_db()
    today = datetime.today()
    today = today.strftime("%m%d%Y")
    mydb = myclient["aws_inventories"]
    instance_col = "ec2_list_" + today
    instance_col = mydb[instance_col]
    return mydb, instance_col
def mongo_export_to_file(interactive, aws_account, aws_account_number,instance_col=None):
    create_directories()
    today = datetime.today()
    today = today.strftime("%m-%d-%Y")
    if not instance_col:
        _, _, instance_col = set_db()
        print(f"Instance Collection: {instance_col}
Interactive: {interactive}")
        time.sleep(10)
    # make an API call to the MongoDB server
    if interactive == 0:
        print(f"Instance Collection: {instance_col}
Interactive: {interactive}")
        time.sleep(10)
        mongo_docs = instance_col.find()
    else:
        mongo_docs = instance_col.find({"Account Number": aws_account_number})
    docs = pandas.DataFrame(mongo_docs)
    docs.pop("_id")
    output_dir = os.path.join("..", "..", "output_files", "aws_instance_list", "csv", "")
    output_file = os.path.join(output_dir, "aws-instance-master-list-" + today +".csv")

These are the collections in MongoDB:

> use aws_inventories
switched to db aws_inventories
> show collections
ec2_list_01082021
ec2_list_01112021
ec2_list_01122021

I'm naming the collections this way so they can be retrieved by date.

The purpose of the print_reports() function is to print reports for old entries from the database. By default the mongo_export_to_file() function prints a report for today's entry and that works fine.

Why can't mongodb find the collection name if I pass it into the function?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...