<jupyter_start><jupyter_text>another mongodb tutorial http://alexrdouglas.com/pymongo-tutorial/<jupyter_code>import pymongo
from pymongo import MongoClient
client = pymongo.MongoClient('this_mongo_1')
client.database_names()
test_db = client.get_database('test2')
test_collection = test_db['test_collection']
test_collection.count() #get number of documents<jupyter_output><empty_output><jupyter_text>add<jupyter_code>items = []

items.append({'name': 'bandit', 'type': 'cat', 'age': 7})
items.append({'name': 'alfred', 'type': 'dog', 'age': 5})
items.append({'name': 'lillie', 'type': 'cat', 'age': 9})

for item in items:
    test_collection.insert_one(item)
    
test_collection.count()<jupyter_output><empty_output><jupyter_text>delete<jupyter_code>test_collection.delete_many({'type': 'cat'})
test_collection.count()<jupyter_output><empty_output><jupyter_text>edit<jupyter_code>for doc in test_collection.find():
    if doc['type'] == 'cat':
        test_collection.update_one({'_id': dock['_id']},
                                  {'$set': {'sound': 'meow'}})
    else:
        test_collection.update_one({'_id': doc['_id']},
                                  {'$set': {'sound': 'woof'}})
        
for doc in test_collection.find():
    print(doc)
import pprint
for doc in test_collection.find():
    pprint.pprint(doc)<jupyter_output>{'_id': ObjectId('5b366623085fb800217cc73f'),
 'age': 5,
 'name': 'alfred',
 'sound': 'woof',
 'type': 'dog'}<jupyter_text>bulk insert<jupyter_code>items = []

items.append({'name': 'bandit', 'type': 'cat', 'age': 7})
items.append({'name': 'alfred', 'type': 'dog', 'age': 5})
items.append({'name': 'lillie', 'type': 'cat', 'age': 9})

for item in items:
    test_collection.insert_one(item)
    
for doc in test_collection.find():
    pprint.pprint(doc)<jupyter_output>{'_id': ObjectId('5b366623085fb800217cc73f'),
 'age': 5,
 'name': 'alfred',
 'sound': 'woof',
 'type': 'dog'}
{'_id': ObjectId('5b3666a5085fb800217cc741'),
 'age': 7,
 'name': 'bandit',
 'type': 'cat'}
{'_id': ObjectId('5b3666a5085fb800217cc742'),
 'age': 5,
 'name': 'alfred',
 'type': 'dog'}
{'_id': ObjectId('5b3666a5085fb800217cc743'),
 'age': 9,
 'name': 'lillie',
 'type': 'cat'}<jupyter_text>bulk update<jupyter_code>for doc in test_collection.find():
    if doc['type'] == 'cat':
        test_collection.update_one({'_id': doc['_id']},
                                  {'$set': {'sound': 'meow'}})
    else:
        test_collection.update_one({'_id': doc['_id']},
                                  {'$set': {'sound': 'woof'}})
for doc in test_collection.find():
    pprint.pprint(doc)<jupyter_output>{'_id': ObjectId('5b366623085fb800217cc73f'),
 'age': 5,
 'name': 'alfred',
 'sound': 'woof',
 'type': 'dog'}
{'_id': ObjectId('5b3666a5085fb800217cc741'),
 'age': 7,
 'name': 'bandit',
 'sound': 'meow',
 'type': 'cat'}
{'_id': ObjectId('5b3666a5085fb800217cc742'),
 'age': 5,
 'name': 'alfred',
 'sound': 'woof',
 'type': 'dog'}
{'_id': ObjectId('5b3666a5085fb800217cc743'),
 'age': 9,
 'name': 'lillie',
 'sound': 'meow',
 'type': 'cat'}<jupyter_text>delete based on criteria<jupyter_code>for doc in test_collection.find():
    if doc['age'] <= 7:
        test_collection.delete_one({'_id': doc['_id']})
        
for doc in test_collection.find():
    pprint.pprint(doc)<jupyter_output>{'_id': ObjectId('5b3666a5085fb800217cc743'),
 'age': 9,
 'name': 'lillie',
 'sound': 'meow',
 'type': 'cat'}<jupyter_text>more detailed lesson . http://altons.github.io/python/2013/01/21/gentle-introduction-to-mongodb-using-pymongo/  begining scripts to load fitbit log files to mongodb<jupyter_code>import json
import os
import pprint
%pwd
os.chdir('/notebooks/GetFitbitData/logs/activities/')
print(os.getcwd)
%pwd
file ='activitiesRate2015-02-20.json'
with open(file) as f:
    sample_data = json.loads(f.read())
    pprint.pprint(sample_data)
pprint.pprint(sample_data.keys())
pprint.pprint(sample_data.items())<jupyter_output>dict_items([('activities', []), ('summary', {'activityLevels': [{'distance': 0, 'minutes': 791, 'name': 'sedentary'}, {'distance': 4.56, 'minutes': 210, 'name': 'lightly'}, {'distance': 0.31, 'minutes': 7, 'name': 'moderately'}, {'distance': 3.93, 'minutes': 47, 'name': 'very'}], 'calories': {'bmr': 1623, 'total': 2739}, 'customHeartRateZones': [], 'distance': 8.79, 'elevation': 42.67, 'floors': 14, 'heartRateZones': [{'caloriesOut': 1901.81106, 'max': 86, 'min': 30, 'minutes': 1293, 'name': 'Out of Range'}, {'caloriesOut': 739.08712, 'max': 121, 'min': 86, 'minutes': 119, 'name': 'Fat Burn'}, {'caloriesOut': 43.8594, 'max': 147, 'min': 121, 'minutes': 6, 'name': 'Cardio'}, {'caloriesOut': 9.22172, 'max': 220, 'min': 147, 'minutes': 2, 'name': 'Peak'}], 'steps': 11965})])<jupyter_text>example of one document to be added to a collection<jupyter_code>day = []

with open(file) as f:
    sample_data = json.loads(f.read())

    key = ['steps']
    value = [sample_data['summary']['steps']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['caloriesOut']
    value = [sample_data['summary']['calories']['total']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['Activity_sedentary']
    value = [sample_data['summary']['activityLevels'][0]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['Activity_lightly']
    value = [sample_data['summary']['activityLevels'][1]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['Activity_moderately']
    value = [sample_data['summary']['activityLevels'][2]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['Activity_very']
    value = [sample_data['summary']['activityLevels'][3]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['HRateOutOfRange']
    value = [sample_data['summary']['heartRateZones'][0]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['HRateFatBurn']
    value = [sample_data['summary']['heartRateZones'][1]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['HRateCardio']
    value = [sample_data['summary']['heartRateZones'][2]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

    key = ['HRatePeak']
    value = [sample_data['summary']['heartRateZones'][3]['minutes']]
    item = dict(zip(key, value))
    day.append(item)

#steps = dict(zip(stepsKey, steps))
pprint.pprint(day)<jupyter_output>[{'steps': 11965},
 {'caloriesOut': 2739},
 {'Activity_sedentary': 791},
 {'Activity_lightly': 210},
 {'Activity_moderately': 7},
 {'Activity_very': 47},
 {'HRateOutOfRange': 1293},
 {'HRateFatBurn': 119},
 {'HRateCardio': 6},
 {'HRatePeak': 2}]
{'fraction_non_alphanumeric': 0.2105499775213547, 'fraction_numerical': 0.052450172336280534, 'mean_word_length': 5.089416058394161, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '624466', 'license': 'MIT', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2495, 'n_words': 468, 'path': 'notebooks/mongoHelp/mongoLesson_with_log_json.ipynb', 'repo_name': 'dguardia/pyhtonForDataScience-David', 'size': '6673'}