I want to use MongoDB GridFS via PHP. I am creating a connection following.
$m = new MongoDBDriverManager("mongodb://127.0.0.1:27017")
This code works in a simple insert. How I can use GridFS? The PHP driver needs to change or the existing PHP driver is fine.
I am using the following:
PHP 7.1.33
MongoDB version v3.6.3
MongoDB extension for PHP driver
MongoDB extension version 1.6.1
Code for migration in MySQL to MongoDB
public function documentupload($offset = 0)
{
// Export MySQL to MongoDB
$this->autoRender = false;
// MongoDB connection
$m = new MongoDBDriverManager("mongodb://127.0.0.1:27017");
$this->loadModel('Documentupload');
// MySQL query to retrieve data
$documents = $this->Documentupload->find('all', array(
'limit' => 1,
'offset' => $offset,
));
// Loop through export data in MongoDB
foreach ($documents as $row) {
echo (int)$row['Documentupload']['id'];
echo "<br/>";
$bulk = new MongoDBDriverBulkWrite;
$document1 = array(
"id" => (int)$row['Documentupload']['id'],
"documentfile" => new MongoDBBSONBinary($row['Documentupload']['documentfile'],
MongoDBBSONBinary::TYPE_GENERIC)
);
$bulk->insert($document1);
$m->executeBulkWrite('agrilicense.documentupload', $bulk);
}
}
2
Answers
Finally I have found any solution. I want to share. All PHP extension version is okay.
Go to github mongodb library here all documentations are available.
https://github.com/mongodb/mongo-php-library
Create a blank folder and run
If Composer not install first install composer. then put vendor folder in your project. after that include autoload.php file in your file. After included this file mongodb office site tutorial work fine.
https://docs.mongodb.com/php-library/v1.8/tutorial/
I am working in cakephp so my code like.
Here test is my database name. fs.chunks and fs.file automatic crated through mongodb.
To use
GridFS
, you’ll need to useBucket
class like this. Reference GridFSThis will create an fs.files document the following abridged data:
I’ve tried this locally and it’s working code
MongoDB Community Server – 3.6
mongodb/mongodb php library – 1.6.1
MongoDB Driver version (pecl) – 1.6.1
MongoDB PHP Extension Module