Skip to content

Instantly share code, notes, and snippets.

def load_data(collection, n=100):
for i,d in load_data_file(n):
collection.insert( d )
def get_data(collection, query={}):
for d in collection.find(query):
yield d
# Load the entire contents of the collection in memory.
elements = []
"distance" : 45344411.211476855,
"path" : [
"North Pole",
"Tokyo",
"Osaka",
"Beijing",
"Shanghai",
"Guangzhou",
"Delhi",
"Mumbai",
function visit ( path ) {
// get last city visited
var city = path.visited[ path.visited.length - 1 ];
var paths = [];
db.edges.find( { from: city } ).forEach (
function( edge ) {
// avoid cities I've already visited
if ( path.visited.indexOf( edge.dest ) == -1 ) {
function Path(){
this.distance = 0;
this.visited = [];
this.clone = function() {
var newPath = new Path();
newPath.distance = this.distance;
for ( i in this.visited )
newPath.visited.push ( this.visited[i] );
return newPath;
};
{
"_id" : ObjectId("5676f18a66414e88e89a09c3"),
"from" : "Delhi",
"dest" : "Beijing",
"dist" : 3776477.26147437
}
{"_id":"Beijing","population":1.952e+07,"location":{"type":"Point","coordinates":[116.383333,39.916667]}}
{"_id":"Delhi","population":2.4953e+07,"location":{"type":"Point","coordinates":[77.23,28.61]}}
{"_id":"Guangzhou","population":2.0597e+07,"location":{"type":"Point","coordinates":[113.266667,23.133333]}}
{"_id":"Mexico City","population":2.0843e+07,"location":{"type":"Point","coordinates":[-99.133333,19.433333]}}
{"_id":"Mumbai","population":2.0741e+07,"location":{"type":"Point","coordinates":[72.825833,18.975]}}
{"_id":"New York","population":1.8591e+07,"location":{"type":"Point","coordinates":[-74.0059,40.7127]}}
{"_id":"North Pole","population":1.0,"location":{"type":"Point","coordinates":[0.0,90.0]}}
{"_id":"Osaka","population":2.0123e+07,"location":{"type":"Point","coordinates":[135.502222, 34.693889]}}
{"_id":"Shanghai","population":2.2991e+07,"location":{"type":"Point","coordinates":[121.5,31.2]}}
{"_id":"São Paulo","population":2.0831e+07,"location":{"type":"Point","coordinates":[-46.633333,-23.5
{
name: "Norberto Leite"
behaved: true
presents: [{_id: "play_that_box_34432", ...}]
}
db.children.aggregate( {"$match": { "behaved": true }},
{"$lookup": { "from": "presents", "localField": "name", "foreignField": "present_for", "as":"presents" } })
// presents collection
> db.presents.find()
{
_id: "5535345_Maneki Neko",
shape: "Round"
receiver: "Shinobu Tsukasa",
street: "〒東京都中央区 日本橋3-1-15, 1F",
city: "Tokio"
zip: "103-0027 - TK",
geo: {
// collection children
> db.children.find()
{
name: "Norberto Leite",
behaved: true,
note: "Deserves all the presents possible!"
}
{
name: "Ze Pequeno",
behaved: false,