MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Using MongoDB

Connect to MongoDB


Create a MongoDB connection, that later you can query:

$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');


In the next example, you will learn how to query the connection object. This extension close the connection automatically, it's not necessary to close manually.

Get multiple documents - find()


Example for searching multiple users with the name "Mike":

$filter = ['name' => 'Mike'];
$query = new \MongoDB\Driver\Query($filter);
$cursor = $manager->executeQuery('database_name.collection_name', $query);
foreach ($cursor as $doc) {
 var_dump($doc);
}


Get one document - findOne()


Example for searching just one user with a specific id, you should do:

$options = ['limit' => 1];
$filter = ['_id' => new \MongoDB\BSON\ObjectID('578ff7c3648c940e008b457a')];
$query = new \MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('database_name.collection_name', $query);
$cursorArray = $cursor->toArray();
if(isset($cursorArray[0])) {
 var_dump($cursorArray[0]);
}

Insert document


Example for adding a document:

$document = [
 'name' => 'John',
 'active' => true,
 'info' => ['genre' => 'male', 'age' => 30]
];
$bulk = new \MongoDB\Driver\BulkWrite;
$_id1 = $bulk->insert($document);
$result = $manager->executeBulkWrite('database_name.collection_name', $bulk);


Update a document


Example for updating all documents where name is equal to "John":

$filter = ['name' => 'John'];
$document = ['name' => 'Mike'];
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->update(
 $filter,
 $document,
 ['multi' => true]
);
$result = $manager->executeBulkWrite('database_name.collection_name', $bulk);


Delete a document


Example for deleting all documents where name is equal to "Peter":

$bulk = new \MongoDB\Driver\BulkWrite;
$filter = ['name' => 'Peter'];
$bulk->delete($filter);
$result = $manager->executeBulkWrite('database_name.collection_name', $bulk);


Conclusion

In this page (written and validated by ) you learned about PHP Using MongoDB . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Using Redis with PHP.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.