Skip to content

Instantly share code, notes, and snippets.

View jmikola's full-sized avatar
💭
🌭

Jeremy Mikola jmikola

💭
🌭
View GitHub Profile
@jmikola
jmikola / foo.php
Created March 20, 2013 22:00
Benchmarking findOne() vs. find()->limit(1)->count(true)
<?php
$m = new MongoClient();
$c = $m->test->foo;
$c->drop();
$c->insert(['_id' => 1, 'x' => str_repeat('z', 1024*1024*4)]);
$c->insert(['_id' => 2, 'x' => str_repeat('z', 1024*1024*4)]);
$c->insert(['_id' => 3, 'x' => str_repeat('z', 1024*1024*4)]);
$c->insert(['_id' => 4, 'x' => str_repeat('z', 1024*1024*4)]);
$c->insert(['_id' => 5, 'x' => str_repeat('z', 1024*1024*4)]);
@jmikola
jmikola / BadIdeaIterator.php
Created January 25, 2013 16:48
Testing iterator implementations that return non-scalars for keys. See: https://jira.mongodb.org/browse/PHP-570
<?php
class BadIdeaIterator implements Iterator
{
protected $position;
protected $keys;
protected $values;
public function __construct(array $keys, array $values)
{
<?php
$mongoClient = new MongoClient('mongodb://localhost');
$collection = $mongoClient->selectCollection('mydb', 'customers');
// Assume $parms exists as an associative array (perhaps from $_POST)
$rp = 'listcustomer';
switch ($rp) {
case 'listcustomers':
@jmikola
jmikola / rs_failover.php
Created December 18, 2012 18:23
MongoDB replica set administration using the PHP driver
<?php
/**
* Testing replica set step down and node removal
*
* Assumes a three-member replica set is running:
*
* mongod --port 2000 --dbpath /data/rs0-db0 --replSet rs0
* mongod --port 2001 --dbpath /data/rs0-db1 --replSet rs0
* mongod --port 2002 --dbpath /data/rs0-db2 --replSet rs0
@jmikola
jmikola / gridfs_batch.php
Created August 10, 2012 17:35
Test batch insertion of documents and referenced GridFS files
#!/usr/bin/env php
<?php
require_once 'config.php'; // mongodb-odm/tools/sandbox/config.php
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* Test batch insertion of documents and referenced GridFS files.
*
@jmikola
jmikola / FindInvalidReferencesCommand.php
Created July 27, 2012 21:47
Find invalid references among Doctrine MongoDB ODM documents
<?php
namespace Acme\FooBundle\Command;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class FindInvalidReferencesCommand extends ContainerAwareCommand
@jmikola
jmikola / reads.js
Created July 26, 2012 20:02
Compare Mongo read behavior
/**
* Initialize the database collection for querying.
*/
var init = function() {
db.foo.drop();
for (var i = 0; i < 10; ++i) {
for (var j = i; j < 100000; j += 10) {
db.foo.insert({x: j, y: j});
}
}
@jmikola
jmikola / shell_output.txt
Created July 13, 2012 14:50
MongoRegex with $all and $in
> db.foo.find({ name: { $all : [/^c/, /^d/] } });
{ "_id" : ObjectId("5000358de84df15c27000000"), "name" : [ "john", "martin", "chuck", "dave" ] }
{ "_id" : ObjectId("5000358de84df15c27000001"), "name" : [ "craig", "don", "dan" ] }
@jmikola
jmikola / dump.php
Created June 21, 2012 22:24
Testing bson_decode() and bson_encode() in PHP
<?php
// dump.php [database] [collection] [filename]
function dump(MongoCollection $collection, $filename) {
$file = fopen($filename, 'w');
foreach ($collection->find() as $document) {
fwrite($file, bson_encode($document));
}
@jmikola
jmikola / benchmark.php
Created May 29, 2012 18:19
Mongo indexing benchmark
<?php
$m = new Mongo();
$c = $m->test->foo;
$c->drop();
$a = microtime(true);
foreach (range(1,1000000) as $i) {
$c->insert(array('x' => $i));
}