Accessing Objects

I am new to using MongoDB and I am currently struggling with the db.find() command. I am trying to query my user database and retrieve a list of all users using Python…

I can get the list of my users by using the following command.

users = db.user.find()

where I am and if I list the variable users I get the following:

for obj in users:
    print(obj)

Yields the following:

bossmandave@Davids-MacBook-Pro-2 kld9-bazaar % cd "/Users/bossmandave/Library/Mobile Documents/com~apple~CloudDocs/KLD9 Bazaar/kld9-bazaar"
bossmandave@Davids-MacBook-Pro-2 kld9-bazaar % /usr/local/bin/python3.9 "/Users/bossmandave/Library/Mobile Documents/com~apple~CloudDocs/KLD9 Bazaar/k
ld9-bazaar/main.py"
{'_id': ObjectId('61358b63af884f0bacbd8775'), 'email': 'johnDoe2@abc123.com', 'phone': '123456789', 'preferences': ['pref 1', 'pref 2'], 'status': 'private', 'uid': 'E943F20A-82B6-4FF2-8460-F8D369E6C51B', 'cart_id': None, 'date_created': datetime.datetime(1, 1, 1, 0, 0), 'date_updated': datetime.datetime(1, 1, 1, 0, 0), 'first_name': 'John', 'hashed_password': None, 'is_Oowner': True, 'is_admin': False, 'last_name': 'Doe2', 'user_created': None, 'user_updated': None, 'address': {'street': '123 2nd St.', 'street1': '', 'city': 'Jacksonville', 'state': 'Florida', 'postal': '49504', 'country': 'United States'}, 'to_delete': False, 'card': {'name_on_card': 'John Doe2', 'card_number': Decimal128('1111222233334444'), 'card_type': 'credit', 'card_issuer': 'Mastercard', 'expiry_date': datetime.datetime(2027, 12, 31, 16, 0), 'cvc_code': 123, 'is_primary': True}}
{'_id': ObjectId('61358b72af884f0bacbd8776'), 'uid': 'B38AC513-06F6-47A5-9AFC-2B38645D492D', 'phone': '123456789', 'email': 'johnDoe3@abc123abc.com', 'preferences': ['pref 1', 'pref 2'], 'cartID': None, 'status': 'private', 'card': {'name_on_card': 'John Doe3', 'card_number': Decimal128('1111222233334444'), 'card_type': 'cedit', 'card_issuer': 'Visa', 'expiry_date': datetime.datetime(2025, 5, 14, 16, 0), 'cvc_code': 456, 'is_primary': True}, 'date_created': datetime.datetime(1, 1, 1, 0, 0), 'date_updated': datetime.datetime(2021, 9, 12, 8, 0, 17, 351000), 'first_name': 'John', 'hashed_password': None, 'is_admin': False, 'is_owner': False, 'last_name': 'Doe3', 'to_delete': False, 'user_created': None, 'user_updated': None, 'address': {'street': '123 3rd St.', 'street2': '', 'city': 'Bankok', 'state': '', 'country': 'Thailand', 'postal': 259859}}
bossmandave@Davids-MacBook-Pro-2 kld9-bazaar % 

But I cannot get the program to print each record out ?? I can’t seem to get into each object and print out data according to the object.

I am quite sure this is a simple fix, but I cannot seem to get it. What am I doing wrong. My initial thinking is that I may need to use a dictionary? Am I on the right track?

Your code seems okay.

You only print 2 documents because you only have 2 documents in your collection named user.

You may print a single field, like email, from obj with

print( obj.get( 'email' ) )

@steevej ,

Thank you sooo much!!! Now, I have another question… If I wanted to format my query into individual records, would I declare my variable as an array and then unwind it? Or is this the only way for me to access the records?

Thanks again,

Dave

@steevej ,
One more question… If you look at the object output, I have two nested objects within the user. Address and Card. I can get the address to print out using user.get(‘address’) which prints the entire object, How do I select specific fields within the nested object? I tried user.get(‘address.street’) to print only the street from the address object, but I got a return of none.

now if I change it to the user.get(‘address’) it returns the entire object.

Thanks for your time,

Dave

First, I am not a python programmer. I really hate python because it uses an invisible character (space or tab which one? I don’t know) to mark blocking. They made the same well know error as make(1) creators made 50 years ago. At least, they did not made the same error as JS because all literals have to be in quotes.

The dot notation

is a mongo thing, not python.

An object like address is the same thing as an object like user.

While user.get(‘first_name’) gives you the field first_name of the object user, user.get(‘address’) gives your the field address of the object user but user.get(‘address’) is a object and you use get() to get its fields. So
user.get(‘address’).get(‘street’) gives you the field street of the object address which is the field address of the object user. If you use a few fields from the user’s object address, it is good practice to do

address = user.get( 'address' )
street = address.get('street')
city = address.get('city')

One you get a MongoDB document in a python variable like user, everything that follows is plain standard python and has nothing to do with MongoDB.

@steevej ,
When I started to learn, I also really didn’t like the fact that it counts white space. And indentation counts in Python, not {}. That being said, now that I have had a chance to “delve” into it for some time (granted I’m a true beginner) I like it just for its portability. But that is my opinion.

I really appreciate your help. I am now getting it and I understand much more on accessing objects than before. I appreciate your patience with me and I’m truly grateful for your tidbits.

Thanks,

Dave

1 Like