{"id":2469,"date":"2025-03-07T23:08:13","date_gmt":"2025-03-07T15:08:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2469"},"modified":"2025-03-09T13:17:17","modified_gmt":"2025-03-09T05:17:17","slug":"mongodb-php-%e6%89%a9%e5%b1%95","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/07\/mongodb-php-%e6%89%a9%e5%b1%95\/","title":{"rendered":"MongoDB PHP \u6269\u5c55"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">MongoDB \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u95e8\u7684 PHP \u6269\u5c55\uff08<code>mongodb<\/code>\uff09\uff0c\u4f7f\u5f97 PHP \u5f00\u53d1\u8005\u80fd\u591f\u4e0e MongoDB \u6570\u636e\u5e93\u8fdb\u884c\u9ad8\u6548\u4ea4\u4e92\u3002\u8fd9\u4e2a\u6269\u5c55\u662f\u7531 MongoDB \u5b98\u65b9\u63d0\u4f9b\u7684\uff0c\u652f\u6301 MongoDB 4.x \u548c\u66f4\u9ad8\u7248\u672c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5b89\u88c5 MongoDB PHP \u6269\u5c55<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 MongoDB \u6269\u5c55\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5 MongoDB PHP \u9a71\u52a8\u7a0b\u5e8f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 Composer \u6216\u76f4\u63a5\u4ece PECL \u5b89\u88c5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u901a\u8fc7 Composer \u5b89\u88c5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 Composer \u5b89\u88c5 MongoDB PHP \u6269\u5c55\u7684\u65b9\u5f0f\u6700\u4e3a\u63a8\u8350\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require mongodb\/mongodb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c06\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5 MongoDB PHP \u6269\u5c55\u4ee5\u53ca\u6240\u6709\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u901a\u8fc7 PECL \u5b89\u88c5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4ece PECL \u5b89\u88c5 MongoDB \u6269\u5c55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pecl install mongodb<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\uff0c\u5728\u4f60\u7684 PHP \u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u662f <code>php.ini<\/code>\uff09\u4e2d\u542f\u7528\u8be5\u6269\u5c55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extension=mongodb.so<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u8fde\u63a5\u5230 MongoDB \u6570\u636e\u5e93<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5\u597d\u6269\u5c55\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 MongoDB \u7684 PHP \u5ba2\u6237\u7aef\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u8fde\u63a5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nrequire 'vendor\/autoload.php';  \/\/ \u5f15\u5165 Composer \u81ea\u52a8\u52a0\u8f7d\u6587\u4ef6\n\n$client = new MongoDB\\Client(\"mongodb:\/\/localhost:27017\");  \/\/ \u8fde\u63a5\u5230 MongoDB \u5b9e\u4f8b\n\n\/\/ \u9009\u62e9\u6570\u636e\u5e93\n$database = $client-&gt;selectDatabase('test');\n\n\/\/ \u9009\u62e9\u96c6\u5408\n$collection = $database-&gt;selectCollection('users');\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u8fde\u63a5 URI<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fde\u63a5\u5b57\u7b26\u4e32\u652f\u6301\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mongodb:\/\/username:password@host1,host2\/?options<\/code><\/li>\n\n\n\n<li>\u8ba4\u8bc1\u3001\u8fde\u63a5\u6c60\u3001SSL \u7b49\u9009\u9879\u90fd\u53ef\u4ee5\u5728\u8fde\u63a5\u5b57\u7b26\u4e32\u4e2d\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. CRUD \u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u63d2\u5165\u6587\u6863<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$document = &#91;'name' =&gt; 'Alice', 'age' =&gt; 25, 'city' =&gt; 'New York'];\n$insertResult = $collection-&gt;insertOne($document);\necho \"Inserted with Object ID '{$insertResult-&gt;getInsertedId()}'\\n\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u67e5\u8be2\u6587\u6863<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u67e5\u8be2\u4e00\u4e2a\u6587\u6863\u5e76\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$document = $collection-&gt;findOne(&#91;'name' =&gt; 'Alice']);\necho \"Found document: \" . json_encode($document) . \"\\n\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u66f4\u65b0\u6587\u6863<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u65b0\u4e00\u4e2a\u5339\u914d\u6761\u4ef6\u7684\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$updateResult = $collection-&gt;updateOne(\n    &#91;'name' =&gt; 'Alice'],\n    &#91;'$set' =&gt; &#91;'age' =&gt; 26]]\n);\necho \"Matched {$updateResult-&gt;getMatchedCount()} document(s)\\n\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5220\u9664\u6587\u6863<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5220\u9664\u4e00\u4e2a\u6587\u6863\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$deleteResult = $collection-&gt;deleteOne(&#91;'name' =&gt; 'Alice']);\necho \"Deleted {$deleteResult-&gt;getDeletedCount()} document(s)\\n\";\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u67e5\u8be2\u591a\u4e2a\u6587\u6863<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$documents = $collection-&gt;find(&#91;'city' =&gt; 'New York']);\nforeach ($documents as $document) {\n    echo json_encode($document) . \"\\n\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u805a\u5408\u67e5\u8be2<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB PHP \u6269\u5c55\u4e5f\u652f\u6301\u805a\u5408\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u805a\u5408\u7ba1\u9053\u6765\u6267\u884c\u590d\u6742\u7684\u67e5\u8be2\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pipeline = &#91;\n    &#91;'$match' =&gt; &#91;'age' =&gt; &#91;'$gte' =&gt; 20]]],\n    &#91;'$group' =&gt; &#91;'_id' =&gt; null, 'averageAge' =&gt; &#91;'$avg' =&gt; '$age']]]\n];\n\n$aggregation = $collection-&gt;aggregate($pipeline);\nforeach ($aggregation as $result) {\n    echo json_encode($result) . \"\\n\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u4f7f\u7528\u8fc7\u6ee4\u5668<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB PHP \u6269\u5c55\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8fc7\u6ee4\u5668\uff0c\u652f\u6301\u591a\u79cd\u6761\u4ef6\u64cd\u4f5c\u7b26\uff08\u5982 <code>eq<\/code>, <code>gt<\/code>, <code>lt<\/code>, <code>in<\/code>, <code>ne<\/code>, <code>exists<\/code> \u7b49\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u8fc7\u6ee4\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nuse MongoDB\\BSON\\Regex;\n\n$cursor = $collection-&gt;find(&#91;'name' =&gt; 'Alice']);\nforeach ($cursor as $document) {\n    echo json_encode($document) . \"\\n\";\n}\n\n\/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\n$cursor = $collection-&gt;find(&#91;'name' =&gt; new Regex('^A')]);\nforeach ($cursor as $document) {\n    echo json_encode($document) . \"\\n\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u4f7f\u7528\u7d22\u5f15<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB PHP \u6269\u5c55\u5141\u8bb8\u4f60\u5728\u96c6\u5408\u4e0a\u521b\u5efa\u7d22\u5f15\u6765\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u521b\u5efa\u7d22\u5f15<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$collection-&gt;createIndex(&#91;'name' =&gt; 1]);  \/\/ \u521b\u5efa\u4e00\u4e2a\u5355\u5b57\u6bb5\u5347\u5e8f\u7d22\u5f15\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u4f7f\u7528\u4e8b\u52a1<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB PHP \u6269\u5c55\u652f\u6301\u591a\u6587\u6863\u4e8b\u52a1\uff08MongoDB 4.x \u5f15\u5165\uff09\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 PHP \u5b9e\u73b0\u4e8b\u52a1\u6765\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e8b\u52a1\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$session = $client-&gt;startSession();\n$session-&gt;startTransaction();\n\ntry {\n    $collection-&gt;insertOne(&#91;'name' =&gt; 'Bob', 'age' =&gt; 30], &#91;'session' =&gt; $session]);\n    $collection-&gt;updateOne(&#91;'name' =&gt; 'Alice'], &#91;'$set' =&gt; &#91;'age' =&gt; 26]], &#91;'session' =&gt; $session]);\n\n    \/\/ \u63d0\u4ea4\u4e8b\u52a1\n    $session-&gt;commitTransaction();\n    echo \"Transaction committed\\n\";\n} catch (Exception $e) {\n    \/\/ \u5982\u679c\u53d1\u751f\u5f02\u5e38\uff0c\u56de\u6eda\u4e8b\u52a1\n    $session-&gt;abortTransaction();\n    echo \"Transaction aborted\\n\";\n}\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u5f02\u6b65\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136 MongoDB PHP \u6269\u5c55\u672c\u8eab\u4e0d\u652f\u6301\u5f02\u6b65\u64cd\u4f5c\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7 ReactPHP \u7b49\u5e93\u4e0e MongoDB \u914d\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u4f7f\u7528 MongoDB PHP \u6269\u5c55\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u9ad8\u8d1f\u8f7d\u7684\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u4f7f\u7528 MongoDB \u7684\u8fde\u63a5\u6c60\u529f\u80fd\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u51cf\u5c11\u6bcf\u6b21\u67e5\u8be2\u7684\u8fde\u63a5\u5efa\u7acb\u5f00\u9500\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1aMongoDB \u6269\u5c55\u63d0\u4f9b\u4e86\u5b8c\u5584\u7684\u5f02\u5e38\u673a\u5236\uff0c\u56e0\u6b64\u4f60\u5e94\u8be5\u9002\u5f53\u6355\u83b7 <code>MongoDB\\Driver\\Exception\\RuntimeException<\/code> \u6216 <code>MongoDB\\Driver\\Exception\\ConnectionException<\/code> \u7b49\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u52a1\u652f\u6301<\/strong>\uff1a\u591a\u6587\u6863\u4e8b\u52a1\u662f MongoDB 4.x \u5f15\u5165\u7684\u65b0\u7279\u6027\uff0c\u4f7f\u7528\u4e8b\u52a1\u65f6\u9700\u8981\u786e\u4fdd MongoDB \u7684\u7248\u672c\u662f 4.0 \u6216\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB PHP \u6269\u5c55\u4e3a PHP \u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u63a5\u53e3\u6765\u4e0e MongoDB \u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8be5\u6269\u5c55\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u6267\u884c CRUD \u64cd\u4f5c\u3001\u805a\u5408\u67e5\u8be2\u3001\u4e8b\u52a1\u7b49\u529f\u80fd\uff0c\u63d0\u5347\u4e86 PHP \u5f00\u53d1\u4e2d\u5bf9 MongoDB \u7684\u652f\u6301\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u95e8\u7684 PHP \u6269\u5c55\uff08mongodb\uff09\uff0c\u4f7f\u5f97 PHP \u5f00\u53d1\u8005\u80fd\u591f\u4e0e MongoDB  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2554,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-2469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2469"}],"version-history":[{"count":3,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2469\/revisions"}],"predecessor-version":[{"id":2556,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2469\/revisions\/2556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2554"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}