Skip to content

Instantly share code, notes, and snippets.

@gatesvp
Created April 1, 2011 20:52
Show Gist options
  • Select an option

  • Save gatesvp/898836 to your computer and use it in GitHub Desktop.

Select an option

Save gatesvp/898836 to your computer and use it in GitHub Desktop.
Tests a php multi-update with MongoDB
Original Collection
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000000
)
[owner] => 1
[type] => question
[v] => 1
)
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000001
)
[owner] => 2
[type] => question
[v] => 1
)
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000002
)
[owner] => 1
[type] => question
[v] => 2
)
Updated Collection
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000000
)
[owner] => 1
[type] => abc
[v] => 1
)
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000001
)
[owner] => 2
[type] => question
[v] => 1
)
Array
(
[_id] => MongoId Object
(
[$id] => 4d963aa56803fa8318000002
)
[owner] => 1
[type] => abc
[v] => 2
)
<?php
try{
$m = new Mongo('localhost:6907');
$db = $m->abc;
$collection = $db->def;
$collection->drop();
// Insert test data
$collection->insert( array('owner' => 1, 'type' => 'question', 'v' => 1) );
$collection->insert( array('owner' => 2, 'type' => 'question', 'v' => 1) );
$collection->insert( array('owner' => 1, 'type' => 'question', 'v' => 2) );
// Print data in system
print("Original Collection\n");
$cursor = $collection->find();
foreach($cursor as $data){
print_r($data);
}
// Perform your update
$collection->update(
array('owner' => 1, 'type' => 'question'),
array('$set' => array('type' => 'abc') ),
array('multiple' => true)
);
print("\nUpdated Collection\n");
$cursor = $collection->find();
foreach($cursor as $data){
print_r($data);
}
}
catch(Exception $e){
print_r($e->getMessage());
print("\n");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment