Guest User

Untitled

a guest
Jun 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. ### Connect
  2. - - -
  3. ```
  4. use MongoDB\Client;
  5. $con = new Client("mongodb://127.0.0.1:27017", array("username" => "xxxx", "password" => "yyyy"));
  6. ```
  7. ### Find
  8. - - -
  9. ```
  10. $filter = ['field' => 'value'];
  11. $option = ['sort' => ['_id' => -1 ], 'limit' => 10];
  12. # -1 is desc, 1 is asc
  13. $data = $con->database->collection->find($filter,$option);
  14. foreach($data as $value){
  15. echo $value['_id'];
  16. }
  17. ```
  18.  
  19. ### Insert
  20. - - -
  21. #### Insert many row
  22. ```
  23. #Connect Database
  24. $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017", array("username" => "xxxx", "password" => "yyyyy"));
  25. $bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]);
  26. #Preparing data
  27. $bulk->insert([
  28. 'field1' => 'value1',
  29. 'field2' => 'value2',
  30. 'field3' => 'value3'
  31. ]);
  32. $bulk->insert([
  33. 'field1' => 'value4',
  34. 'field2' => 'value5',
  35. 'field3' => 'value6'
  36. ]);
  37.  
  38. #Insert data
  39. try {
  40. $result = $manager->executeBulkWrite('database.collection', $bulk);
  41. } catch (MongoDB\Driver\Exception\BulkWriteException $e) {
  42. var_dump($e->getWriteResult()->getWriteErrors());
  43. }
  44. ```
  45. Null, not null and compare
  46. ```
  47. #Where field not null
  48. 'field' => ['$exists' => true ]
  49. ```
  50. ### Update
  51. - - -
  52. ### UpdateOne
  53. - - -
  54. ```
  55. $updatedResult = $collection->updateOne(
  56. ['_id' => new MongoDB\BSON\ObjectId($id)],
  57. ['$set' => [
  58. 'field1' => 'value1',
  59. 'field2' => 'value2',
  60. ]
  61. );
  62. ```
  63. ### Delete
  64. - - -
Add Comment
Please, Sign In to add comment