Skip to content

Instantly share code, notes, and snippets.

@sindbach
sindbach / uncapCollectionExample.cpp
Created September 24, 2015 23:13
A simple cpp example to uncap a capped collection in MongoDB. This version is using the legacy mongo-cxx-driver https://github.com/mongodb/mongo-cxx-driver.
#include "mongo/client/dbclient.h"
int main() {
/* Initialize Driver */
mongo::Status status = mongo::client::initialize();
if (!status.isOK()){
std::cout<< "Failed to initialize driver." << std::endl;
return EXIT_FAILURE;
}
@sindbach
sindbach / insertSubDocumentExample.c
Created September 25, 2015 00:17
A simple C example to insert a sub document in MongoDB.
#include <bson.h>
#include <mongoc.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
bson_error_t error;
@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))
@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 / 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 / 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 / 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 / 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 / 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 / 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");