Common functions of the PHP operation MongoDB

Source: Internet
Author: User

Connect to MongoDB:

$MONGOOBJ New Array (                        ' connect ' = =true,                        ' persist ' =true                    ));

Select a library:

$mongoDB $mongoObj->selectdb ("Wxdata");

Select a collection:

$mongoColletion=$mongoDB->selectcollection ("Apachelog");

To delete a collection:

$mongoColletion->drop ();

Insert document data:

$mongoColletion->insert (array("FirstName" = "Bob", "LastName" and "Jones"));

To modify the updated document data:

$newdata=Array(' $set ' =Array("Address" = "1 Smith Lane"));$mongoColletion->update (Array("FirstName" = "Bob"),$newdata);$mongoColletion-Update (Array("uri" = "/summer_pics"),Array(' $inc ' =Array("page hits" = 1)),Array("Upsert" =true)    );$mongoColletion-Update (Array("Name" = "Joe"),Array("username" = "joe312", "createdat" + =NewMongodate ()),Array("Upsert" =true)    );$today=Array(' $gt ' =NewMongodate (), ' $lt ' =NewMongodate (Strtotime("+1 Day")));$mongoColletion-Update (Array("Birthday" =$today),Array(' $set ' =Array(' Gift ' =$surprise)),Array("Multiple" =true)    );

New Document data:

$obj Array (' x ' = 1); $mongoColletion->save ($obj); $obj [' foo '] = ' bar '; // $obj cannot be inserted again, resulting in duplicate _id error//$mongoColletion->insert ($obj); $mongoColletion->save ($obj);

To remove a document data:

$mongoColletion->remove (arrayarraytrue));

Number of statistical documents:

$mongoColletion,count(array(' Httpstatus ' =>304));

To query a document:

$mongoColletion->findone (array("httpstatus" = 200));

To view document index information:

$mongoColletion->getindexinfo ();

To build a document index:

//Create an index on ' httpstatus ' ascending$mongoColletion->ensureindex (Array(' httpstatus ' = 1));//Create a unique index on ' size '$mongoColletion->ensureindex (Array(' size ' = 1),Array(' unique ' =true));//Create a compound index on ' size ' ascending and ' status ' descending$mongoColletion->ensureindex (Array(' Size ' = 1, ' status ' = 1));//Full-Text indexing$mongoColletion-Ensureindex (Array(            ' Title ' = ' text ', ' desc ' = ' text ',        ),Array(            ' Name ' = ' exampletextindex ', ' weights ' =Array(                ' title ' = +, ' desc ' = 30,            )        )    );

Close the connection to MongoDB:

$mongoObj->close ();

There are so many functions that you can use from PHP to manipulate MongoDB.

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.