This article mainly introduces PHP to operate MongoDB to realize the add, delete, modify and query functions. It summarizes and analyzes the common techniques of operating MongoDB in PHP in the form of examples. It also comes with the method of operating MongoDB in php7, which has certain reference value. Friends who need it can refer to it.
The example in this article describes the PHP operation of MongoDB to implement the add, delete, modify and query functions. Share it with everyone for your reference, the details are as follows:
MongoDB’s PHP driver provides some core classes to operate MongoDB. Generally speaking, it can implement all the functions in the MongoDB command line, and the format of the parameters Basically similar. Versions before PHP7 and versions after PHP7 have different operations on MongoDB. This article mainly uses the previous version of PHP7 as an example to explain the various operations of PHP on MongoDB. Finally, it briefly explains the operations of MongoDB on PHP7 and later versions.
1. Data insertion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
2. Data Query
1. Query a single document:
1 2 3 4 5 6 7 8 9 |
|
2. Query multiple documents:
1 2 3 4 5 6 7 8 9 10 11 |
|
Define queries using various conditional operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Use other functions provided by the MongoCursor class:
1 2 3 4 5 6 7 8 9 10 |
|
Aggregate query: group data for statistics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
3. Data modification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
##4. Data deletion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
PHP7 operation method
Data insertion:
1 2 3 4 5 6 7 8 |
|
Data query:
1 2 3 4 5 6 7 8 9 |
|
Data modification:
1 2 3 4 5 6 7 8 9 10 |
|
##Data deletion:
1 2 3 4 5 6 7 8 |
|
Articles you may be interested in:
Common tips for PHP operation Redis Summary of PHP tipsAnalysis of php skills on the method of reading and writing excel files in native phpAnalysis of php skills on the principles and implementation methods of the singleton mode of PHP design patternThe above is the detailed content of PHP operates MongoDB to implement addition, deletion, modification and query functions. PHP skills. For more information, please follow other related articles on the PHP Chinese website!