PHP code for common MongoDB operations

Source: Internet
Author: User
PHP code for common MongoDB operations

  1. $ Conn = new Mongo ();
  2. // Can be abbreviated
  3. // $ Conn = new Mongo (); # connect to the local host, default port.
  4. // $ Conn = new Mongo ("172.21.15.69"); # connect to the remote host
  5. // $ Conn = new Mongo ("xiaocai. loc: 10086"); # connect to the remote host on the specified port
  6. // $ Conn = new Mongo ("xiaocai. loc", array ("replicaSet" => true); # server load balancer
  7. // $ Conn = new Mongo ("xiaocai. loc", array ("persist" => "t"); # persistent connection
  8. // $ Conn = new Mongo ("mongodb: // sa: 123 @ localhost"); # Password with user name
  9. # Select test Database
  10. $ Db = $ conn-> test;
  11. // $ Db = $ conn-> selectDB ("test"); # Method 2
  12. # Select a set (select "table ")
  13. $ Collection = $ db-> user;
  14. // $ Collection = $ db-> selectCollection ("user"); # Method 2
  15. # Insert operation
  16. $ Data = array ("uid" => "zz123", "user_name" => "James ");
  17. $ Result = $ collection-> insert ($ data); # Simple insert
  18. Echo "id of the inserted data". $ data ["_ id"];
  19. Exit;
  20. # Insert operation security insert
  21. $ Data = array ("uid" => "zz124", "user_name" => "Li Si ");
  22. $ Result = $ collection-> insert ($ data, true); # Used to Wait for MongoDB to complete the operation to determine whether the operation is successful. (This parameter is useful when a large number of records are inserted)
  23. # Modification operation
  24. $ Where = array ("uid" => "zz123 ");
  25. $ Newdata = array ("user_name" => "Zhang Sansan", "tel" => "123456789 ");
  26. $ Result = $ collection-> update ($ where, array ('$ set' => $ newdata ));
  27. # Replace updates
  28. $ Where = array ("uid" => "zz124 ");
  29. $ Newdata = array ("user_age" => "22", "tel" => "123456789 ");
  30. $ Result = $ collection-> update ($ where, $ newdata );
  31. # Batch update
  32. $ Where = array ("uid" => 'zz ');
  33. $ Newdata = array ("user_name" => "zz", "money" = & gt; 1000 );
  34. $ Result = $ collection-> update ($ where, array ('$ set' => $ newdata), array ('multiple' => true ));
  35. # Automatically accumulate
  36. $ Where = array ('Money' => 1000 );
  37. $ Newdata = array ('User _ name' => 'Edit ');
  38. $ Result = $ collection-> update ($ where, array ('$ set' => $ newdata, '$ Inc' => array ('Money' =>-5 )));
  39. # Deleting a node
  40. $ Where = array ('uid' => 'zz124 ');
  41. $ Result = $ collection-> update ($ where, array ('$ unset' => 'Tel'); // delete a node tel
  42. # Deleting data
  43. $ Collection-> remove (array ('uid' => 'zz124 '));
  44. # Deleting a specified consumer ID
  45. $ Id = new consumer id ('4d638ea1d549a028038511 ');
  46. $ Collection-> remove (array ('_ id' => (object) $ id ));
  47. # Query data. note: $ gt is greater than, $ gte is greater than or equal to, $ lt is less than, $ lte is less than or equal to, $ ne is not equal to, and $ exists does not exist.
  48. Echo 'Count: '. $ collection-> count ()."
    "; # All
  49. Echo 'Count: '. $ collection-> count (array ('uid' => 'zz123 '))."
    "; # Conditions can be added
  50. Echo 'Count :'. $ collection-> count (array ('age' => array ('$ gt' => 10, '$ lte' => 30 )))."
    "; # Greater than 50 less than or equal to 74
  51. Echo 'Count: '. $ collection-> find ()-> limit (5)-> skip (0)-> count (true )."
    "; # Obtain the actual number of returned results
  52. # All documents in the collection
  53. $ Cursor = $ collection-> find ()-> snapshot ();
  54. Foreach ($ cursor as $ id => $ value ){
  55. Echo "$ id:"; var_dump ($ value );
  56. Echo"
    ";
  57. }
  58. # Querying a piece of data
  59. $ Cursor = $ collection-> findOne ();
  60. # Exclude columns. if the value is false, the column is not displayed.
  61. $ Cursor = $ collection-> find ()-> fields (array ("age" => false, "tel" => false ));
  62. # Set true to display
  63. $ Cursor = $ collection-> find ()-> fields (array ("user_name" => true ));
  64. # (Tel, age node exists) and age! = 0 and age <50
  65. $ Where = array ('tel' => array ('$ exists' => true), 'age' => array (' $ ne '=> 0, '$ lt' => 50, '$ exists' => true ));
  66. $ Cursor = $ collection-> find ($ where );
  67. # Retrieve result sets by page
  68. $ Cursor = $ collection-> find ()-> limit (5)-> skip (0 );
  69. # Sorting
  70. $ Cursor = $ collection-> find ()-> sort (array ('age' =>-1, 'type' => 1 )); #1 indicates descending order-1 indicates ascending order, and the order of parameters is affected.
  71. # Index
  72. $ Collection-> ensureIndex (array ('age' => 1, 'Money' =>-1); #1 indicates descending order-1 indicates ascending order
  73. $ Collection-> ensureIndex (array ('age' => 1, 'Money' =>-1), array ('background' => true )); # create indexes in the background for running (synchronous operation by default)
  74. $ Collection-> ensureIndex (array ('age' => 1, 'Money' =>-1), array ('unique' => true )); # This index is unique.
  75. # Obtain query results
  76. $ Cursor = $ collection-> find ();
  77. $ Array = array ();
  78. Foreach ($ cursor as $ id => $ value ){
  79. $ Array [] = $ value;
  80. }
  81. # Closing a connection
  82. $ Conn-> close ();
  83. ?>


MongoDB, PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.