Skip to content

Instantly share code, notes, and snippets.

Created March 10, 2015 14:42
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
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
for file in glob.glob("*-sleep.json"):
# just some output showing which data is being processed
print ("------------------------------------")
# load JSON data
json_data = open(file)
data = json.load(json_data)
# insert JSON into MongoDB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment