PHP Operations MongoDB Notes ____php

Source: Internet
Author: User
Tags connect mongo create index findone mongoclient mongodb

Connect MONGO
$connection  = new  mongoclient ();  Connect to localhost:27017
$connection  = new  mongoclient (  "mongodb://example.com"  );  Connect to a remote server (using the default port: 27017)
$connection  = new  mongoclient (  "mongodb://example.com:65432"  );  Linking to a remote server, using a custom port
This driver uses a persistent connection and reuses it the next time it tries to link to the same server.

Validate
Specifying the username and password in the connection URI (preferred)
$m  = new  mongoclient ("mongodb://$ {username}: ${password} @localhost ");
Specifying the username and password via the options array (alternative)
$m  = new  mongoclient ("mongodb:// localhost ", Array (" username "  =>  $username,  " password "  =>  $password));
Specifying the authentication database in the connection URI (preferred)
$m  = new  mongoclient ("MongoDB ://${username}: ${password} @localhost/mydatabase ");
Specifying the authentication database via the options array (alternative)
$m  = new  mongoclient ("MONGO db://${username}: ${password} @localhost ", Array (" DB "  =>  " MyDatabase "));

fragmentation (cluster)
$m  = new  mongoclient ("mongodb://mongos1.example.com:27017,mongos2.example.com:27017"));

Copy
Use the "replicaset" option to specify the name of the duplicate. The same name is represented in a cluster. Multiple servers are separated by commas.
Using multiple servers as the seed list (prefered)
$m  = new  mongoclient ("mongodb://rs1.example.com:27017 , rs2.example.com:27017/?replicaset=myreplsetname "));
Using one server as the seed list 
$m  = new  mongoclient ("mongodb://rs1.example.com:27017", Array ("Replic Aset "=>" "Myreplsetname")  );
Using multiple servers as the seed list
$m  = new  mongoclient ("mongodb://rs1.example.com:27017, rs2.example.com:27017 ", Array (" Replicaset "  =>  " Myreplsetname "));

Get database Instance
$connection  = new  mongoclient ();
$db  =  $connection-> dbname;

Get Collection Instance
$connection  = new  mongoclient ();
$db  =  $connection-> baz;

Select a collection:
$collection  =  $db-> foobar;

Or, directly selecting a database and collection:
$collection  =  $connection-> baz->;

Insert a document

Use the Mongocollection::insert () method:

$doc  = Array (
     "name"  =>  "MongoDB",
     "type"  =>  "Database",
     "Count"  =>  1,
     "info"  => (object) Array (  "x"  =>  203,  "y"  =>  102),
     "versions"  => Array ("0.9.7",  "0.9.8",  "0.9.9")
);
$collection-> Insert (  $doc  );

using the Mongocollection::findone () method
$document  =  $collection-> findone ();

To Add multiple documents:
For (  $i  =  0;  $i  <  ;  $i + +) {
     $collection-> Insert Array (  ' i '  =>  $i,  field {$i} "  =>  $i  *  2  ) );
}

calculate the number of documents:
echo  $collection-> count ();

use cursors to get all documents
All documents in the collection to live, we need the Mongocollection::find () method. The Find () method returns a Mongocursor object that allows us to iterate through the entire result set to read the document.
$cursor  =  $collection-> find ();
foreach (  $cursor  as  $id  =>  $value  ) {
    echo  "$id:";
    Var_dump (  $value  );

Set query Criteria
$query  = Array (  ' i '  =>  );
$cursor  =  $collection-> Find (  $query  );
while (  $cursor-> hasnext ()) {
     var_dump (  $cursor-> getNext ());
}

Establish an index
$collection-> ensureindex (Array ("I" => 1)); Create index on "i"
$collection-> ensureindex (Array ("I" =>-1, "J" => 1)); Index on "I" Descending, "J" Ascending

SQL to MONGO corresponding table
SQL query Statement MONGO Query Statement
CREATE TABLE USERS (a number, b number) Implicitly created, or mongodb::createcollection ().
INSERT into USERS VALUES (1,1) $db->users->insert (Array ("A" => 1, "B" => 1));
SELECT A,b from users $db->users->find (Array (), Array ("A" => 1, "B" => 1));
SELECT * from Users WHERE age=33 $db->users->find (Array ("age" => 33));
SELECT a,b from users WHERE age=33 $db->users->find (Array ("Age" =>), Array ("A" => 1, "B" => 1));
SELECT a,b from Users WHERE age=33 order by name $db->users->find (Array ("Age" =>), Array ("A" => 1, "B" => 1))->sort (Array ("name" => 1));
SELECT * from Users WHERE age>33 $db->users->find (Array ("Age" => Array (' $GT ' => 33));
SELECT * from Users WHERE age<33 $db->users->find (Array ("Age" => Array (' $lt ' => 33));
SELECT * from the users WHERE name like "%joe%" $db->users->find (Array ("name" => new Mongoregex ("/joe/"));
SELECT * from the users WHERE name like "joe%" $db->users->find (Array ("name" => new Mongoregex ("/^joe/"));
SELECT * from Users WHERE age>33 and age<=40 $db->users->find (Array ("Age" => Array (' $GT ' =>, ' $lte ' => 40));
SELECT * from users ' ORDER by name DESC $db->users->find ()->sort (Array ("name" =>-1));
CREATE INDEX myindexname on users (name) $db->users->ensureindex (Array ("name" => 1));
CREATE INDEX myindexname on users (Name,ts DESC) $db->users->ensureindex (Array ("name" => 1, "ts" =>-1));
SELECT * from Users WHERE a=1 and b= ' Q ' $db->users->find (Array ("A" => 1, "B" => "Q"));
SELECT * from users LIMIT SKIP 20 $db->users->find ()->limit->skip (20);
SELECT * from users WHERE a=1 or b=2 $db->users->find (Array (' $or ' => Array ("a" => 1), Array ("B" => 2)));
SELECT * from users LIMIT 1 $db->users->find ()->limit (1);
EXPLAIN SELECT * from users WHERE z=3 $db->users->find (Array ("Z" => 3))->explain ()
SELECT DISTINCT last_name from users $db->command (Array ("distinct" => "users", "Key" => "last_name"));
SELECT COUNT (*y) from users $db->users->count ();
SELECT COUNT (*y) from the users where age > 30 $db->users->find (Array ("Age" => Array (' $GT ' =>))->count ();
SELECT COUNT (age) from users $db->users->find (Array ("Age" => Array (' $exists ' => true))->count ();
UPDATE users SET a=1 WHERE b= ' q ' $db->users->update (Array ("B" => "Q"), Array (' $set ' => Array ("a" => 1));
UPDATE users SET a=a+2 WHERE b= ' q ' $db->users->update (Array ("B" => "Q"), Array (' $inc ' => Array ("a" => 2));
DELETE from users WHERE z= "ABC" $db->users->remove (Array ("Z" => "abc"));



Related Article

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.