Skip to content

Instantly share code, notes, and snippets.

View RWaltersMA's full-sized avatar

Rob Walters RWaltersMA

View GitHub Profile
@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 / 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 / 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):
@RWaltersMA
RWaltersMA / main.py
Last active September 3, 2018 23:58
Main.py file for Implementing an end-to-end IoT Solution in MongoDB. Used with NodeMCU and MicroPython
from umqtt.simple import MQTTClient
import network
import machine
import time
import socket
import utime
ssid=#Put your SSID name here
pwd=#Put your WiFi password here
device_name='TempSensor1' #Put the name of the device here
@RWaltersMA
RWaltersMA / ProdReviewFunc.cs
Last active November 9, 2020 14:42
Azure Function - HTTP Trigger that leverages MongoDB Atlas database
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
@RWaltersMA
RWaltersMA / gateway.py
Created September 4, 2018 00:05
Gateway Python file for MongoDB IoT end to end use case
import hashlib;
import hmac;
import datetime;
import paho.mqtt.client as mqtt
import json
import calendar
import requests
mqqt_server=#enter your mqqt server here
mqqt_port=1883
@RWaltersMA
RWaltersMA / webhook.js
Created September 5, 2018 12:58
MongoDB Stitch webhook for IoT blog
exports = function(payload,response) {
const mongodb = context.services.get("mongodb-atlas");
const sensordata = mongodb.db("iotdb").collection("sensordata");
var body = {};
if (payload.body) {
try{
{"exportVersion":4,"dashboards":{"dashboard-1":{"description":"","filters":[],"layout":[{"w":9,"h":2,"x":0,"y":2,"i":"item-1","minW":1,"maxW":10,"minH":1,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true},{"w":9,"h":2,"x":0,"y":4,"i":"item-2","minW":1,"maxW":10,"minH":1,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true},{"w":2,"h":2,"x":0,"y":0,"i":"item-3","minW":1,"maxW":10,"minH":1,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true},{"i":"item-4","x":2,"y":0,"w":2,"h":2,"minW":1,"minH":1,"maxW":10,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true},{"i":"item-5","x":4,"y":0,"w":2,"h":2,"minW":1,"minH":1,"maxW":10,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true}],"title":"kafkaPerformanceSource"}},"items":{"item-1":{"allowInteractiveFilters":true,"calculatedFields":[],"channels":{"x":{"channelType":"category","field":"timestampColumn","inferredType":"Date","type":"nominal","
@RWaltersMA
RWaltersMA / sink-chart-performance.charts
Created February 2, 2022 23:15
MongoDB Chart showing MongoDB Connector for Apache Kafka Sink Performance
{"exportVersion":4,"dashboards":{"dashboard-1":{"description":"","filters":[{"type":"Date","disabled":false,"name":"_insertedTS","settings":{"type":"absolute","absolute":{"from":{"enabled":true,"date":"2021-07-27","time":"17:30:00","inclusive":true},"to":{"enabled":false,"date":null,"time":"00:00:00","inclusive":false},"timezone":"Europe/Paris"},"relative":{"from":{"enabled":false,"tense":null,"value":"","unit":null},"until":{"enabled":false,"tense":null,"unit":"days","value":""}},"period":{"direction":"previous","count":"1","unit":"day","includeCurrent":true}},"linkedFields":[{"dataSourceId":"data-source-1","fieldPath":"_insertedTS"}]}],"layout":[{"w":10,"h":2,"x":0,"y":1,"i":"item-1","minW":1,"maxW":10,"minH":1,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true},{"w":10,"h":2,"x":0,"y":3,"i":"item-2","minW":1,"maxW":10,"minH":1,"maxH":10,"moved":false,"static":false,"isDraggable":true,"isResizable":true},{"w":2,"h":1,"x":0,"y":0,"i":"item-3","minW":1,"maxW":10,"minH":1,"maxH":10,"m