Php and mongoDB operation example _ PHP Tutorial

Source: Internet
Author: User
Tags findone
Detailed descriptions of php and mongoDB operation examples. MongoDB is a database stored in json format and is very suitable for various application development. let me introduce some mongoDB learning instances to you. To integrate PHP, mongodb database is a database stored in json format. it is very suitable for various application development. I will introduce some mongoDB learning instances to you.


Mongodb needs to install the Mongo extension to integrate PHP. this is relatively simple. let's talk about the MongoDB PHPAPI and usage.
Let's take a look at a simple example:

Php code

1.

2. $ m = new Mongo (); // use the default port 27017 to connect to the local machine. of course, you can also connect to a remote host such as 192.168.0.4: 27017. if the port is 27017, the port can be omitted.

3.

4. $ db = $ m-> comedy; // select the comedy Database. if the database is not created before, it can be created automatically. you can also use $ m-> selectDB ("comedy ");

5.

6. $ collection = $ db-> collection; // select the collection set in comedy, which is equivalent to the table in RDBMS. you can also use

7. $ db-> selectCollection ("collection ");

8.

9. $ obj = array ("title" => "Calvin and Hobbes", "author" => "Bill Watterson ");

10.

11. $ collection-> insert ($ obj); // add $ obj to $ collection

12.

13.

14. $ obj = array ("title" => "XKCD", "online" => true );

15. $ collection-> insert ($ obj );

16. $ cursor = $ collection-> find ();

17.

18. foreach ($ cursor as $ obj) {// traverse all documents in the set

19. echo $ obj ["title"]. "n ";

20 .}

21.

22.

23. $ m-> close (); // disconnect MongoDB

The following describes some common functions:

Php code

1. $ query = array ("I" => 71 );

2. $ cursor = $ collection-> find ($ query); // search for documents that meet $ query in the $ collectio collection

3.

4. while ($ cursor-> hasNext ()){

5. var_dump ($ cursor-> getNext ());

6 .}

7.

8. $ collection-> findOne (); // return the first document in the $ collection set

9.

10. $ collection-> count (); // returns the number of documents in the $ collection.

11. $ coll-> ensureIndex (array ("I" => 1); // add an index in descending order for I "this column"

12. $ coll-> ensureIndex (array ("I" =>-1, "j" => 1 )); // add an index to sort j in ascending order for I "this column"

During query, each Object is automatically inserted with a unique _ id, which is equivalent to the primary key in RDBMS and convenient for query.
For example:

Php code

1.

2.

3. $ person = array ("name" => "joe ");

4.

5. $ people-> insert ($ person );

6.

7. $ joe = $ people-> findOne (array ("_ id" => $ person ['_ id']);

8.

9.?>


Update:

Assume that we want to modify the author name in comments in the following document.

Php code

1 .{

2. "_ id": ObjectId ("4b06c282edb87a281e09dad9 "),

3. "content": "this is a blog post .",

4. "comments ":

5 .[

6 .{

7. "author": "Mike ",

8. "comment": "I think that blah ...",

9 .},

10 .{

11. "author": "John ",

12. "comment": "I disagree ."

13 .}

14.]

15 .}

To change an internal domain, we use $ set (to ensure that other fields in the document are not removed) and the comment index also changes.

Php code

1.

2.

3. $ collection-> update ($ criteria, array ('$ set' => array ("comments.1" => array ("author" => "Jim ")))); // $ criteria is the element to be updated

4.

5.?>


Delete a database

Php code

1. $ m-> dropDB ("comedy ");

List all available databases

Php code

1. $ m-> listDBs (); // no return value
Well, I will write so much. if you are interested, you can find other Mongo-php API usage on the Internet!

Command line instance:

1. db. system. users. find ()

2. db. users. count ()

3. db. users. ensureIndex ({password:-1 })

4. use test

5. db. users. getIndexes ()

6. db. repairDatabase ()

7. show users

8. show dbs

9. db. users. find ({username: {$ in: ['4d81a82398790']}). explain ()

10. db. users. dropIndexes ()

11. db. users. find (). count ()

12. db. users. find (). limit (5)

13. db. users. find ({"username": "ssa "})

14. show collections

15. db. users. remove ()

16. db. user. remove ({'username': 'admin '})

17. db. user. insert ({'username': 'admin', 'age': 21, 'nickname': 'admin '})

18. db. user. save ({'username': 'admin', 'age': 21, 'info': ['12', '123', 'zzsd']})

19. db. createCollection ("user ")

20. db. dropDatabase ()

21. show collections

22. db. test. drop ()

23. db. copyDatabase ('test', 'test1 ')

24. show profile

25. db. printCollectionStats ()

26. db. addUser ('admin', 'admin123 ')

27. db. setProfilingLevel (2 );

28. db. setProfilingLevel (1, 10 );

29. db. system. profile. find ()

Bytes. Mongodb needs to integrate 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.