Skip to content

Instantly share code, notes, and snippets.

@sindbach
sindbach / aggregation.js
Last active June 19, 2020 23:38
Compare two object documents in MongoDB 4.4 with $function aggregation pipeline
/*
Outputs:
{
"_id": ObjectId("5eec4fd0aee4bf9d2e209e2d"),
"diff": {
"haveMore": {
"foo2": null
},
"types": {
"differentType": NumberLong("55"),
@sindbach
sindbach / Program.cs
Last active April 15, 2020 10:35
MongoDB .NET/C# Polymorphism BsonKnownTypes
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Bson.Serialization.Attributes;
using System.Collections.Generic;
namespace Application
{
@sindbach
sindbach / nearsphere_near_difference.js
Created May 5, 2017 04:59
A simple JS test to show the difference between $nearSphere and $near
(function() {
"use strict";
let coll = db.geomap_tests;
coll.drop();
coll.insert({ _id: "Westfield London", location: [ -0.22157, 51.507176 ] });
coll.insert({ _id: "Green Lanes Shopping Centre", location: [ -0.098092, 51.576198 ] });
print("INDEX: 2dsphere QUERY: GeoJSON");
@sindbach
sindbach / initial_sharding_limit.js
Created June 6, 2016 06:40
Shard existing collection JS test
var st = new ShardingTest({shards: 2, mongos: 1, other: { chunkSize: 1 }});
var db = st.s.getDB('test');
var coll = db.world;
/* Create ashard key length (approximate) */
var keyval = "";
var keylength = 512;
/* Create a document of approximately 1MB (minus shard key) */
@sindbach
sindbach / mongodb_lookup.go
Created April 15, 2016 00:40
A simple example of how to use $lookup in Golang using mgo.
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
@sindbach
sindbach / bashrc
Created April 7, 2016 00:38
bashrc file for docker machine.
# timer related
timer_start() { timer=${timer:-$SECONDS}; }
timer_stop() { timer_show=$((${SECONDS} - ${timer})); unset timer;}
BLUE="\[\033[0;36m\]"
NORMAL="\[\033[0m\]"
RED="\[\033[31;1m\]"
YELLOW="\[\033[0;33m\]"
ORANGE="\[\033[38;5;95;38;5;214m\]"
PURPLE="\[\033[38;5;95;38;5;128m\]"
BYELLOW="\[\033[33;1m\]"
@sindbach
sindbach / groupby_count_aggregation.cs
Created March 30, 2016 09:47
A simple C# script to demo MongoDB aggregation performing group by count.
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Threading.Tasks;
using System.Linq;
/* Where document structure stored in localhost MongoDB : {token:"word"}
*/
namespace Aggregation_01
@sindbach
sindbach / README.md
Last active February 17, 2016 04:36 — forked from erh/README.md
sample aggregation stage module - inject

This is a sample aggregation stage that insert a field into every document.

Steps:

  1. Git clone the mongodb source code.
  2. Create a directory called 'modules' in src/mongo/db/modules.
  3. Git clone this repo as src/mongo/db/modules/inject
  4. Build MongoDB
  5. Now try the new aggregation stage:
@sindbach
sindbach / polymorphic_aggregation_model.cs
Last active March 13, 2024 04:27
A simple C# example to demonstrate polymorphic abstract classes to interact with MongoDB aggregation result.
using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
public abstract class MongoModelBase
{
[BsonId]
public ObjectId Id { get; set; }
@sindbach
sindbach / ObjectID_YAML.py
Last active January 13, 2016 06:44
A simple Python example on how to serialise/deserialise MongoDB ObjectID() using PyYAML.
import yaml
from pymongo import MongoClient
from bson import objectid
def objectid_representer(dumper, data):
return dumper.represent_scalar("!bson.objectid.ObjectId", str(data))
def objectid_constructor(loader, data):
return objectid.ObjectId(loader.construct_scalar(data))