Skip to content

Instantly share code, notes, and snippets.

View kevinadi's full-sized avatar

Kevin Adistambha kevinadi

  • MongoDB
  • Sydney, Australia
View GitHub Profile
@kevinadi
kevinadi / mongodb-ssl.sh
Last active March 24, 2025 10:32
Script to create self-signed CA certificates, server certificates, and client certificates for testing MongoDB with SSL
#!/bin/sh
# Generate self signed root CA cert
openssl req -nodes -x509 -newkey rsa:2048 -keyout ca.key -out ca.crt -subj "/C=AU/ST=NSW/L=Sydney/O=MongoDB/OU=root/CN=`hostname -f`/[email protected]"
# Generate server cert to be signed
openssl req -nodes -newkey rsa:2048 -keyout server.key -out server.csr -subj "/C=AU/ST=NSW/L=Sydney/O=MongoDB/OU=server/CN=`hostname -f`/[email protected]"
# Sign the server cert
@kevinadi
kevinadi / MongoDotnetDriver.cs
Created January 5, 2017 22:37
Small example of using MongoDB dotnet driver (dotnet-core, MongoDB.Driver 2.4.1)
using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
namespace ConsoleApplication
{
public class Person {
public ObjectId Id { get; set; }
@kevinadi
kevinadi / MongoJavaDriver.java
Created January 4, 2017 23:50
Small example of using MongoDB Java Driver (v3.4.1)
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
@kevinadi
kevinadi / hanoi.py
Last active December 21, 2016 06:51
Tower of Hanoi
import sys
import unittest
import copy
class HanoiTest(unittest.TestCase):
def test_number_of_steps_3(self):
''' number of steps should be 2^n-1 (3 disks) '''
h = Hanoi(3).solve()
self.assertEqual(len(h), 2**3)
@kevinadi
kevinadi / external-ip.sh
Created June 9, 2016 14:01
What is my external IP address?
curl -s http://ipinfo.io/ip
@kevinadi
kevinadi / mongo-binary.js
Created May 26, 2016 00:22
Save binary file in MongoDB
var mongoBinary = {
save : function(url, collection, filename) {
var fs = require('fs')
var Binary = require('mongodb').Binary
var MongoClient = require('mongodb').MongoClient
var assert = require('assert')
var doc = {
@kevinadi
kevinadi / geodist.hs
Created May 25, 2016 02:06
Distance between two points on a great circle
-- Calculates distances between two points (a,b) and (x,y) on a great circle with radius r
-- Coordinates are in the form of (longitude,latitude)
--
-- Example: calculate distance between points (0,0) and (1,1) on earth:
-- ghci> geodist 6378.1 0 0 1 1
-- 157.42462387232553
--
-- Result should be identical to MongoDB's $geoNear
--
-- Formula was taken from http://andrew.hedges.name/experiments/haversine/
@kevinadi
kevinadi / mongodb-query-plan-types.txt
Created May 12, 2016 23:00
MongoDB: query plan types
$ grep -R "::kStageType" *
src/mongo/db/exec/and_hash.cpp:const char* AndHashStage::kStageType = "AND_HASH";
src/mongo/db/exec/and_sorted.cpp:const char* AndSortedStage::kStageType = "AND_SORTED";
src/mongo/db/exec/cached_plan.cpp:const char* CachedPlanStage::kStageType = "CACHED_PLAN";
src/mongo/db/exec/collection_scan.cpp:const char* CollectionScan::kStageType = "COLLSCAN";
src/mongo/db/exec/count.cpp:const char* CountStage::kStageType = "COUNT";
src/mongo/db/exec/count_scan.cpp:const char* CountScan::kStageType = "COUNT_SCAN";
src/mongo/db/exec/delete.cpp:const char* DeleteStage::kStageType = "DELETE";
src/mongo/db/exec/distinct_scan.cpp:const char* DistinctScan::kStageType = "DISTINCT_SCAN";
src/mongo/db/exec/ensure_sorted.cpp:const char* EnsureSortedStage::kStageType = "ENSURE_SORTED";
@kevinadi
kevinadi / unfoldr.scala
Created May 10, 2016 07:09
unfoldr in Scala
def unfoldr[A, B](seed: B)(func: B => Option[(A, B)]): Stream[A] =
func(seed) match {
case Some((a, b)) => a #:: unfoldr(b)(func)
case None => Stream.empty
}
/*
* Infinite sequence:
* val s = unfoldr (0) (b => Some(b,b+1))
* Fibonacci sequence:
@kevinadi
kevinadi / random_pwd.fish
Created May 2, 2016 06:09
Fish shell: create 5 random passwords
function random_pwd
for i in (seq 5)
cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1 | tr -d '\n' | fold -w 3 | tr '\n' '-'
echo ''
end
end