Last active
May 1, 2017 02:30
-
-
Save RWaltersMA/a006144ea98c805189d5810c4bd2e819 to your computer and use it in GitHub Desktop.
Creating Sample Python code for Python on MongoDB blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
from random import randint | |
#Step 1: Connect to MongoDB - Note: Change connection string as needed | |
client = MongoClient(port=27017) | |
db=client.business | |
#Step 2: Create sample data | |
names = ['Kitchen','Animal','State', 'Tastey', 'Big','City','Fish', 'Pizza','Goat', 'Salty','Sandwich','Lazy', 'Fun'] | |
company_type = ['LLC','Inc','Company','Corporation'] | |
company_cuisine = ['Pizza', 'Bar Food', 'Fast Food', 'Italian', 'Mexican', 'American', 'Sushi Bar', 'Vegetarian'] | |
for x in xrange(1, 501): | |
business = { | |
'name' : names[randint(0, (len(names)-1))] + ' ' + names[randint(0, (len(names)-1))] + ' ' + company_type[randint(0, (len(company_type)-1))], | |
'rating' : randint(1, 5), | |
'cuisine' : company_cuisine[randint(0, (len(company_cuisine)-1))] | |
} | |
#Step 3: Insert business object directly into MongoDB via isnert_one | |
result=db.reviews.insert_one(business) | |
#Step 4: Print to the console the ObjectID of the new document | |
print('Created {0} of 100 as {1}'.format(x,result.inserted_id)) | |
#Step 5: Tell us that you are done | |
print('finished creating 100 business reviews') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the gist has been updated to create 500 entries, the print statements should likely reflect that change as well.