- $ Conn = new Mongo ();
- // Can be abbreviated
- // $ Conn = new Mongo (); # connect to the local host, default port.
- // $ Conn = new Mongo ("172.21.15.69"); # connect to the remote host
- // $ Conn = new Mongo ("xiaocai. loc: 10086"); # connect to the remote host on the specified port
- // $ Conn = new Mongo ("xiaocai. loc", array ("replicaSet" => true); # server load balancer
- // $ Conn = new Mongo ("xiaocai. loc", array ("persist" => "t"); # persistent connection
- // $ Conn = new Mongo ("mongodb: // sa: 123 @ localhost"); # Password with user name
- # Select test Database
- $ Db = $ conn-> test;
- // $ Db = $ conn-> selectDB ("test"); # Method 2
- # Select a set (select "table ")
- $ Collection = $ db-> user;
- // $ Collection = $ db-> selectCollection ("user"); # Method 2
- # Insert operation
- $ Data = array ("uid" => "zz123", "user_name" => "James ");
- $ Result = $ collection-> insert ($ data); # Simple insert
- Echo "id of the inserted data". $ data ["_ id"];
- Exit;
- # Insert operation security insert
- $ Data = array ("uid" => "zz124", "user_name" => "Li Si ");
- $ 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)
- # Modification operation
- $ Where = array ("uid" => "zz123 ");
- $ Newdata = array ("user_name" => "Zhang Sansan", "tel" => "123456789 ");
- $ Result = $ collection-> update ($ where, array ('$ set' => $ newdata ));
- # Replace updates
- $ Where = array ("uid" => "zz124 ");
- $ Newdata = array ("user_age" => "22", "tel" => "123456789 ");
- $ Result = $ collection-> update ($ where, $ newdata );
- # Batch update
- $ Where = array ("uid" => 'zz ');
- $ Newdata = array ("user_name" => "zz", "money" = & gt; 1000 );
- $ Result = $ collection-> update ($ where, array ('$ set' => $ newdata), array ('multiple' => true ));
- # Automatically accumulate
- $ Where = array ('Money' => 1000 );
- $ Newdata = array ('User _ name' => 'Edit ');
- $ Result = $ collection-> update ($ where, array ('$ set' => $ newdata, '$ Inc' => array ('Money' =>-5 )));
- # Deleting a node
- $ Where = array ('uid' => 'zz124 ');
- $ Result = $ collection-> update ($ where, array ('$ unset' => 'Tel'); // delete a node tel
- # Deleting data
- $ Collection-> remove (array ('uid' => 'zz124 '));
- # Deleting a specified consumer ID
- $ Id = new consumer id ('4d638ea1d549a028038511 ');
- $ Collection-> remove (array ('_ id' => (object) $ id ));
- # 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.
- Echo 'Count: '. $ collection-> count ()."
"; # All
- Echo 'Count: '. $ collection-> count (array ('uid' => 'zz123 '))."
"; # Conditions can be added
- Echo 'Count :'. $ collection-> count (array ('age' => array ('$ gt' => 10, '$ lte' => 30 )))."
"; # Greater than 50 less than or equal to 74
- Echo 'Count: '. $ collection-> find ()-> limit (5)-> skip (0)-> count (true )."
"; # Obtain the actual number of returned results
- # All documents in the collection
- $ Cursor = $ collection-> find ()-> snapshot ();
- Foreach ($ cursor as $ id => $ value ){
- Echo "$ id:"; var_dump ($ value );
- Echo"
";
- }
- # Querying a piece of data
- $ Cursor = $ collection-> findOne ();
- # Exclude columns. if the value is false, the column is not displayed.
- $ Cursor = $ collection-> find ()-> fields (array ("age" => false, "tel" => false ));
- # Set true to display
- $ Cursor = $ collection-> find ()-> fields (array ("user_name" => true ));
- # (Tel, age node exists) and age! = 0 and age <50
- $ Where = array ('tel' => array ('$ exists' => true), 'age' => array (' $ ne '=> 0, '$ lt' => 50, '$ exists' => true ));
- $ Cursor = $ collection-> find ($ where );
- # Retrieve result sets by page
- $ Cursor = $ collection-> find ()-> limit (5)-> skip (0 );
- # Sorting
- $ Cursor = $ collection-> find ()-> sort (array ('age' =>-1, 'type' => 1 )); #1 indicates descending order-1 indicates ascending order, and the order of parameters is affected.
- # Index
- $ Collection-> ensureIndex (array ('age' => 1, 'Money' =>-1); #1 indicates descending order-1 indicates ascending order
- $ Collection-> ensureIndex (array ('age' => 1, 'Money' =>-1), array ('background' => true )); # create indexes in the background for running (synchronous operation by default)
- $ Collection-> ensureIndex (array ('age' => 1, 'Money' =>-1), array ('unique' => true )); # This index is unique.
- # Obtain query results
- $ Cursor = $ collection-> find ();
- $ Array = array ();
- Foreach ($ cursor as $ id => $ value ){
- $ Array [] = $ value;
- }
- # Closing a connection
- $ Conn-> close ();
- ?>
|