Skip to content

Instantly share code, notes, and snippets.

View RWaltersMA's full-sized avatar

Rob Walters RWaltersMA

View GitHub Profile
@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 / 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 / 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 / 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 / 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{
@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 / CSharpTransactions.cs
Created October 16, 2018 17:53
Example C# code showing MongoDB Transactions using the MongoDB .NET Driver (2.7+)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDBTransaction
const mongodb = require("mongodb");
run().catch(error => console.error(error));
async function run() {
console.log(new Date(), "Connecting to localhost");
const uri = "mongodb://127.0.0.1:27017/test?replicaSet=repl0";
const client = await mongodb.MongoClient.connect(
uri,
function(err, client) {