Application Study Notes for MongoDB in PHP

Source: Internet
Author: User

1. Connection

The default mongodb port is 27017. Therefore, we can connect to mongodb: $ mongodb = new Mongo ('localhost') or specify the IP address and port $ mongodb = new Mongo ('192. 168.127.1: 27017 ') the port can be changed.

If mongodb enables authentication, that is, -- auth, the connection is: $ mongodb = new Mongo ('root: w888168 @ localhost: 100 ');

2. Select a database

$ Db = $ mongodb-> selectDBs ('dbname'); or you can directly specify the Database Name (because mongodb can directly create a database that does not exist): $ db = $ mongodb-> dbname;

3. Select collection)

$ Tables = $ db-> selectColletion ('tablename') or $ tables = $ db-> tablename;

Next, you can operate the set.

4. Insert a new document)

Insert data into collection, such as personal information

$ One = array (

'Name' => 'lily ';

'Age' => 0,

'Hobby' => array (

'Dance ',

'Swim ',

'Music'

),

);

$ Res = $ tables-> insert ($ one, true );


The second parameter is used to wait for MongoDB to complete the operation to determine whether the operation is successful. The default value is false. this parameter is useful when a large number of records are inserted. After a new document is inserted, MongoDB returns a record ID.

The result is: 1, indicating that the data has been inserted.

5. delete a document

It is relatively simple to delete a document: $ res = $ tables-> remove (array ('name' => 'lily '));

Note: All eligible documents will be deleted here. If you only need to delete the first document that meets the conditions, add the second parameter to true.

6. Search for documents

Define a filter: $ filter = array ('name' => 'lily ');

$ Res = $ tables-> find ($ filter );

Find one: $ res = $ tables-> findOne ($ filter );

In addition, you can add a comparison in the filter.


For example, obtain information with sessions greater than 10:
$ Filter = array ('session' => array ('$ gt' => 10); (gt --> lt -- <gte --> = lte <=)
$ Cursor = $ collection-> find ($ filter );

The returned interface is a cursor.


Note that the query is executed only when the result is required. In the 1st example, the query is executed only when the foreach loop starts.
This is a very useful feature, because it can be used to retrieve the results by Adding options in the cursor (cursor), just this moment before the query is defined and executed. For example, you can set the option to execute paging, or obtain a specified number of matched documents.
$ Total = $ cursor-> total ();
$ Cursor-> limit (20)-> skip (40 );
Foreach ($ cursor as $ user ){
}

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.