Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script that processes a directory of Basis sleep data in JSON format (one file per day) and inserts records into a MongoDB collection
from __future__ import print_function
import pymongo
import glob
import os
import json
# Define mongoDB database and collection to store sleep data
db_name = "mydb"
collection_name = "mysleeps"
# Connect to Mongo DB
try:
con = pymongo.MongoClient()
coll = con.bobapi.sleeps
print ("Connected successfully!!!")
except pymongo.errors.ConnectionFailure, e:
print ("Could not connect to MongoDB: %s" % e)
# 1. place all basis export files into a directory named ./data/basis
# 2. open each sleep data file in the directory and save in MongoDB
os.chdir("./data/basis")
for file in glob.glob("*-sleep.json"):
# just some output showing which data is being processed
print ("------------------------------------")
print(file)
# load JSON data
json_data = open(file)
data = json.load(json_data)
# insert JSON into MongoDB
coll.save(data)
json_data.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.