Skip to content

Instantly share code, notes, and snippets.

View gatesvp's full-sized avatar

Gaëtan Perrault gatesvp

View GitHub Profile
@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
@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')
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 / 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){
@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 / document_digging.js
Created May 3, 2011 00:16
Reaching into documents
var x = {
"_id" : ObjectId("4dbf16ecee3bbe4d22000003"),
"options" : {
"options" : [ { "price" : { "_type" : "decimal", "val" : 1000 }, "label" : "10" }
],
"categories" : [[ "WTF","11" ]]
}
}
db.deals_test.drop();
@gatesvp
gatesvp / nulls.js
Created May 4, 2011 18:30
Test for nulls in MongoDB
> db.foo.insert( { x : 1, y : 1 } )
> db.foo.insert( { x : 2, y : "string" } )
> db.foo.insert( { x : 3, y : null } )
> db.foo.insert( { x : 4 } )
> db.foo.find()
{ "_id" : ObjectId("4dc1974612c677fc83b5629d"), "x" : 1, "y" : 1 }
{ "_id" : ObjectId("4dc1974c12c677fc83b5629e"), "x" : 2, "y" : "string" }
{ "_id" : ObjectId("4dc1975312c677fc83b5629f"), "x" : 3, "y" : null }
{ "_id" : ObjectId("4dc1975a12c677fc83b562a0"), "x" : 4 }
> db.foo.find( { "y" : null } )
@gatesvp
gatesvp / sort_test.php
Created May 9, 2011 17:54
PHP test sorting on sub-document
<?php
try{
$m = new Mongo('localhost');
$db = $m->abc;
$collection = $db->def;
$collection->drop();
// Insert test data
$collection->insert( array('A' => 1, 'B' => 4, 'C' => array("C1" => 'val', "C2" => 15) ) );
@gatesvp
gatesvp / isset.php
Created May 10, 2011 00:09
Sample with isset
<?php
try{
$m = new Mongo('localhost');
$db = $m->abc;
$collection = $db->def;
$collection->drop();
// Insert test data
$collection->insert( array('A' => 1, 'B' => 4, 'C' => array("C1" => 'val', "C2" => 15) ) );
<?php
try{
### Step 1 & 2
$m = new Mongo('ec2-50-16-121-178.compute-1.amazonaws.com');
$db = $m->admin;
### Step 3
$res = $db->command(array('replSetGetStatus' => 1));
print_r($res);