Reputation: 47
I have a problem, my code does not work exactly as I want. I need to create a backup and restore. And that is my problem; My new collection is not identical. Wrong data form:
My backup
$db = $m->$dbname;
try{
$backup1 = $db->people->find();
$text1=json_encode(iterator_to_array($backup1));
$people = 'people.txt';
if ($text1 ){
file_put_contents($people, $text1, LOCK_EX);
echo 'complete';
}
else echo 'error';
My restore
$db = $m->$dbname;
try{
$collection = $db->people1;
$file = file_get_contents('people.txt', FILE_USE_INCLUDE_PATH);
if ($file){
$file = json_decode($file);
foreach ($file as $id => $item) {
$collection->insert($item);
}
echo 'Restore complete';
}
else echo 'Error';
Upvotes: 1
Views: 2333
Reputation: 3090
Use mongodump
and mongorestore
— Back Up and Restore with MongoDB Tools
Upvotes: 2