Skip to content

Instantly share code, notes, and snippets.

View gatesvp's full-sized avatar

Gaëtan Perrault gatesvp

View GitHub Profile
@gatesvp
gatesvp / pivot.js
Created May 2, 2011 20:02
Pivot Actor/Movies into Movie/Actors
db.foo.drop();
db.foo.insert( { actor: "Richard Gere", movies: ['Pretty Woman', 'Runaway Bride', 'Chicago'] })
db.foo.insert( { actor: "Julia Roberts", movies: ['Pretty Woman', 'Runaway Bride', 'Erin Brockovich'] })
map = function() {
for(var i in this.movies){
key = { movie: this.movies[i] };
value = { actors: [ this.actor ] };
emit(key, value);
}
@gatesvp
gatesvp / spaces.php
Created May 1, 2011 03:04
Testing spaces + PHP + mongodb
<?php
$mongo = new Mongo();
$coll = $mongo->selectCollection('foo', 'documents');
$coll->drop();
$coll->insert( array( "bar foo" => array( 'bah baz' => 2 ) ) );
$res = $coll->find(array('bar foo.bah baz' => 2));
foreach($res as $r){
randomString = function(string_length) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
@gatesvp
gatesvp / gist:908554
Created April 7, 2011 19:45
test distinct + geo index
> db.locations.insert({coords: [-79.4434, 44.1428], type: 'bank', tags:['24h', 'finance']})
> db.locations.insert({coords: [-62.0343, 45.4432], type: 'cinema', tags: ['film', 'popcorn']})
> db.locations.insert({coords: [-64.3433, 45.3341], type: 'cafe', tags: ['coffee', 'tea']})
> db.locations.distinct('type')
[ "bank", "cinema", "cafe" ]
> db.locations.ensureIndex({ type : 1 })
> db.locations.distinct('type')
[ "bank", "cafe", "cinema" ]
> db.locations.ensureIndex({coords: '2d', type: 1, tags: 1})
> db.locations.distinct('type')
@gatesvp
gatesvp / output.txt
Created April 1, 2011 20:52
Tests a php multi-update with MongoDB
Original Collection
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000000
)
[owner] => 1
[type] => question
print(' ')
printShardingStatus()
db.runCommand({enablesharding : 'fred_sharded'})
db.runCommand({enablesharding : 'fred_sharded2'})
db.runCommand({shardcollection : 'fred_sharded.temp', key:{'_id':1}})
db.runCommand({shardcollection : 'fred_sharded2.temp', key:{'_id':1}})
db2 = db.getSisterDB('fred_sharded')
db3 = db.getSisterDB('fred_sharded2')
Method One: Array of Objects
------
var pd = { type: "Person", attributes: [{name: "Penelope Doe"},{age: 26}] };
var pd2 = { type: "Person", attributes: [ {name: "Johnny Doe"},{age: 27} ] };
db.foo.drop();
db.foo.save(pd);
db.foo.save(pd2);
db.foo.ensureIndex({attributes:1})
db.foo.find({'attributes' : {age : {$gt : 26 } } }).count()
db.foo.find({'attributes' : {age : {$gt : 26 } } }).explain()
@gatesvp
gatesvp / $set example
Created March 3, 2011 17:46
Create a new data field using set
> var d = {
... drives:{
... C:{
... windows:{
... 'explorer':{
... admin:'r+w'
... }
... }
... }
... }
@gatesvp
gatesvp / subset.js
Created February 16, 2011 18:26
Proper subset function for MongoDB $where clause
// Define function
subset = function (test, superset) {
if (test.length > superset.length) {
return false;
}
for (var i in test) {
found = false;
for (var j in superset) {
found = found || superset[j] == test[i];
}
@gatesvp
gatesvp / powershell_startup.ps1
Created February 16, 2011 00:00
easy powershell aliases
function df {
$colItems = Get-wmiObject -class "Win32_LogicalDisk" -namespace "root\CIMV2" `
-computername localhost
foreach ($objItem in $colItems) {
write $objItem.DeviceID $objItem.Description $objItem.FileSystem `
($objItem.Size / 1GB).ToString("f3") ($objItem.FreeSpace / 1GB).ToString("f3")
}