forked from mongodb-developer/python-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpymongo_test_query.py
38 lines (29 loc) · 1.32 KB
/
pymongo_test_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Get the database using the method we defined in pymongo_test_insert file
from pymongo_test_insert import get_database
dbname = get_database()
# Create a new collection
collection_name = dbname["user_1_items"]
item_details = collection_name.find()
for item in item_details:
# This will give readable output, but KeyError
print(item['item_name'], item['category'])
###---------------------------------------------------###
### Comment the above 'for loop' & 'print statements' ###
### for the next lines of code to work ###
###---------------------------------------------------###
from pandas import DataFrame
# Convert the dictionary objects to dataframe
items_df = DataFrame(item_details)
# View all items
print(items_df)
###--------------------------------------------------------###
### Get items of particular category without and with index###
###--------------------------------------------------------###
item_details = collection_name.find({"category" : "food"})
for item in item_details:
print(item)
# Add more data to understand the need for indexing
import pymongo_test_insert_more_items
# Create index on category, as an example
category_index = collection_name.create_index("category")
# Execute the previous query again to see the documents scanned (refer to the article)