Skip to content

Instantly share code, notes, and snippets.

View RWaltersMA's full-sized avatar

Rob Walters RWaltersMA

View GitHub Profile
@RWaltersMA
RWaltersMA / QueryserverStatus.py
Created April 10, 2017 21:43
Issuing a MongoDB command from PyMongo
from pymongo import MongoClient
# pprint library is used to make the output look more pretty
from pprint import pprint
# connect to MongoDB, change the << MONGODB URL >> to reflect your own connection string
client = MongoClient(<<MONGODB URL>>)
db=client.admin
# Issue the serverStatus command and print the results
serverStatusResult=db.command("serverStatus")
pprint(serverStatusResult)
@RWaltersMA
RWaltersMA / Update.py
Created April 10, 2017 17:10
Update showing effects of default upsert in mongodb PyMongo
from pymongo import MongoClient
#include pprint for readabillity of the
from pprint import pprint
#change the MongoClient connection string to your MongoDB database instance
client = MongoClient(port=27020)
db=client.business
ASingleReview = db.reviews.find_one({})
print('A sample document:')
@RWaltersMA
RWaltersMA / FindWIthAggregation.py
Last active April 14, 2017 20:39
Find in Python using Aggregation Framework
from pymongo import MongoClient
# Connect to the MongoDB, change the connection string per your MongoDB environment
client = MongoClient(port=27017)
# Set the db object to point to the business database
db=client.business
# Showcasing the count() method of find, count the total number of 5 ratings
print('The number of 5 star reviews:')
fivestarcount = db.reviews.find({'rating': 5}).count()
print(fivestarcount)
# Not let's use the aggregation framework to sum the occurrence of each rating across the entire data set
@RWaltersMA
RWaltersMA / CreateSamples.py
Last active May 1, 2017 02:30
Creating Sample Python code for Python on MongoDB blog
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):