bluethundr
bluethundr

Reputation: 1345

Python problem inserting records into MongoDB

I'm building a list of instances in Amazon EC2. My script works great to put the list into a CSV file and mail it to you.

But now I want to insert the records into MongoDB so that differences in the environment can be tracked.

I have a function to insert the records into MongoDB that looks like this:

def insert_col(instance_col,mydict):
    x = instance_col.insert_one(mydict) 
    print(f"MongoDB record inserted: {x.inserted_id}")
    return x

The dictionary called instance_col that I'm trying to insert into the DB looks like this:

{'AWS Account': 'jf-master-pd', 'Account Number': 'xxxxxxxxx', 'Name': 'usawsweb010', 'Instance ID': 'i-0871d82fb5c4e268b', 'AMI ID': 'ami-006219aba10688d0b', 'Volumes': 'vol-047b2ca3dd93b9021, vol-02ea0a81271a00b0b, vol-00abcef2eee9f19de, vol-09222810b7fc52265, vol-03c9a84ee93f81edc', 'Private IP': '172.31.75.47', 'Public IP': 'x.xxx.xxx.xxx', 'Private DNS': 'ip-172-31-75-47.ec2.internal', 'Availability Zone': 'us-east-1f', 'VPC ID': 'vpc-68b1ff12', 'Type': 't2.micro', 'Key Pair Name': 'timd', 'State': 'running', 'Launch Date': 'July 11 2020'}

If I try to use the insert_col function it inserts the first record then gives an exception that says:

MongoDB record inserted: 5f0a46f267a8132e4b31e83f
An exception has occurred: dict contains fields not in fieldnames: '_id'

I don't know why it's complaining about my dict not containing a fieldname called '_id' AFTER the record is inserted. The program stops running after that.

This is the full traceback:

  Traceback (most recent call last):
  File ".\aws_ec2_list_instances.py", line 768, in <module>
    main()
  File ".\aws_ec2_list_instances.py", line 632, in main
    output_file = list_instances(aws_account,aws_account_number, interactive, regions, fieldnames, show_details, instance_col)
  File ".\aws_ec2_list_instances.py", line 249, in list_instances
    writer.writerow(instance_dict)
  File "C:\Users\tdun0002\AppData\Local\Programs\Python\Python38-32\lib\csv.py", line 154, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
  File "C:\Users\tdun0002\AppData\Local\Programs\Python\Python38-32\lib\csv.py", line 149, in _dict_to_list
    raise ValueError("dict contains fields not in fieldnames: "
ValueError: dict contains fields not in fieldnames: '_id'

When I call the function I call it like this: insert_col(instance_col,instance_dict). If I comment it out the program goes on it's merry way and works perfectly like it did before I started monkeying around with MongoDB.

I have 10 servers in this environment that I'm trying to insert into MongoDB.

Why is it doing this and how can I insert these records into MongoDB?

Upvotes: 0

Views: 49

Answers (1)

InfoLearner
InfoLearner

Reputation: 15608

Try doing

from bson.objectid import ObjectId
def insert_col(instance_col,mydict):
    mydict['_id'] = ObjectId() 

    x = instance_col.insert_one(mydict) 
    print(f"MongoDB record inserted: {x.inserted_id}")
    return x


Upvotes: 1

Related Questions