Skip to content

Instantly share code, notes, and snippets.

@btroia
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
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