{"id":21641,"date":"2024-01-02T12:52:26","date_gmt":"2024-01-02T12:52:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21641\/"},"modified":"2024-11-14T10:29:22","modified_gmt":"2024-11-14T02:29:22","slug":"how-to-fix-mongodb-error-code-47-nomatchingdocument","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21641\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument"},"content":{"rendered":"<h1 id=\"how-to-fix-mongodb-error-code-47-nomatchingdocument-TqlUwbFOqP\">\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument<\/h1>\n<p>MongoDB \u662f\u4e00\u500b\u53d7\u6b61\u8fce\u7684\u958b\u6e90 NoSQL \u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u9ad8\u6027\u80fd\u3001\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6027\u3002\u7136\u800c\uff0c\u8207\u5176\u4ed6\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u9700\u8981\u89e3\u6c7a\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u539f\u56e0\u4e26\u63d0\u4f9b\u9010\u6b65\u89e3\u6c7a\u65b9\u6848\u4f86\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-mongodb-error-code-47-nomatchingdocument-TqlUwbFOqP\">\u4e86\u89e3 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument<\/h2>\n<p>\u7576\u67e5\u8a62\u6216\u66f4\u65b0\u64cd\u4f5c\u672a\u80fd\u5728\u6307\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u4efb\u4f55\u5339\u914d\u6587\u6a94\u6642\uff0c\u5c31\u6703\u767c\u751f MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument\u3002\u9019\u500b\u932f\u8aa4\u901a\u5e38\u5728\u4f7f\u7528 <code>findOne()<\/code> \u6216 <code>updateOne()<\/code> \u65b9\u6cd5\u6642\u51fa\u73fe\u3002<\/p>\n<p>\u7576\u57f7\u884c\u67e5\u8a62\u6216\u66f4\u65b0\u64cd\u4f5c\u6642\uff0cMongoDB \u6703\u641c\u7d22\u7b26\u5408\u6307\u5b9a\u689d\u4ef6\u7684\u6587\u6a94\u3002\u5982\u679c\u627e\u4e0d\u5230\u5339\u914d\u7684\u6587\u6a94\uff0cMongoDB \u5c07\u8fd4\u56de NoMatchingDocument \u932f\u8aa4\u3002<\/p>\n<h2 id=\"possible-causes-of-mongodb-error-code-47-nomatchingdocument-TqlUwbFOqP\">MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument \u7684\u53ef\u80fd\u539f\u56e0<\/h2>\n<p>\u60a8\u53ef\u80fd\u6703\u9047\u5230 NoMatchingDocument \u932f\u8aa4\u7684\u539f\u56e0\u6709\u5e7e\u500b\uff1a<\/p>\n<ul>\n<li><strong>\u67e5\u8a62\u689d\u4ef6\u4e0d\u6b63\u78ba\uff1a<\/strong> \u4ed4\u7d30\u6aa2\u67e5\u67e5\u8a62\u689d\u4ef6\uff0c\u78ba\u4fdd\u5b83\u5011\u6e96\u78ba\u5730\u4ee3\u8868\u60a8\u8a66\u5716\u6aa2\u7d22\u6216\u66f4\u65b0\u7684\u6587\u6a94\u3002\u67e5\u8a62\u4e2d\u7684\u5c0f\u932f\u8aa4\u53ef\u80fd\u5c0e\u81f4\u627e\u4e0d\u5230\u5339\u914d\u7684\u6587\u6a94\u3002<\/li>\n<li><strong>\u6578\u64da\u4e0d\u5339\u914d\uff1a<\/strong> \u9a57\u8b49\u60a8\u6b63\u5728\u641c\u7d22\u6216\u66f4\u65b0\u7684\u6578\u64da\u662f\u5426\u8207\u96c6\u5408\u4e2d\u5b58\u5132\u7684\u6578\u64da\u5339\u914d\u3002\u6578\u64da\u985e\u578b\u6216\u503c\u7684\u5dee\u7570\u53ef\u80fd\u5c0e\u81f4\u627e\u4e0d\u5230\u5339\u914d\u7684\u6587\u6a94\u3002<\/li>\n<li><strong>\u7f3a\u5c11\u7d22\u5f15\uff1a<\/strong> \u5982\u679c\u60a8\u5728\u4e00\u500b\u7f3a\u5c11\u7d22\u5f15\u7684\u5b57\u6bb5\u4e0a\u57f7\u884c\u67e5\u8a62\uff0cMongoDB \u53ef\u80fd\u9700\u8981\u6383\u63cf\u6574\u500b\u96c6\u5408\uff0c\u5c0e\u81f4\u627e\u4e0d\u5230\u5339\u914d\u7684\u6587\u6a94\u3002\u8003\u616e\u6dfb\u52a0\u7d22\u5f15\u4ee5\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"how-to-fix-mongodb-error-code-47-nomatchingdocument-TqlUwbFOqP\">\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument<\/h2>\n<p>\u73fe\u5728\u6211\u5011\u4e86\u89e3\u4e86 NoMatchingDocument \u932f\u8aa4\u7684\u53ef\u80fd\u539f\u56e0\uff0c\u8b93\u6211\u5011\u63a2\u8a0e\u4e00\u4e9b\u89e3\u6c7a\u65b9\u6848\u4f86\u4fee\u5fa9\u5b83\uff1a<\/p>\n<h3 id=\"1-review-query-criteria-TqlUwbFOqP\">1. \u6aa2\u67e5\u67e5\u8a62\u689d\u4ef6<\/h3>\n<p>\u4ed4\u7d30\u6aa2\u67e5\u67e5\u8a62\u689d\u4ef6\uff0c\u78ba\u4fdd\u5b83\u5011\u6e96\u78ba\u5730\u4ee3\u8868\u60a8\u8a66\u5716\u6aa2\u7d22\u6216\u66f4\u65b0\u7684\u6587\u6a94\u3002\u6ce8\u610f\u5b57\u6bb5\u540d\u7a31\u3001\u6578\u64da\u985e\u578b\u548c\u6578\u503c\u3002\u6839\u64da\u9700\u8981\u9032\u884c\u8abf\u6574\uff0c\u4ee5\u5339\u914d\u5b58\u5132\u5728\u96c6\u5408\u4e2d\u7684\u6578\u64da\u3002<\/p>\n<h3 id=\"2-check-data-consistency-TqlUwbFOqP\">2. \u6aa2\u67e5\u6578\u64da\u4e00\u81f4\u6027<\/h3>\n<p>\u9a57\u8b49\u60a8\u6b63\u5728\u641c\u7d22\u6216\u66f4\u65b0\u7684\u6578\u64da\u662f\u5426\u8207\u96c6\u5408\u4e2d\u5b58\u5132\u7684\u6578\u64da\u5339\u914d\u3002\u78ba\u4fdd\u6578\u64da\u985e\u578b\u6216\u503c\u4e4b\u9593\u6c92\u6709\u5dee\u7570\u3002\u7cfe\u6b63\u4efb\u4f55\u4e0d\u4e00\u81f4\uff0c\u4ee5\u78ba\u4fdd\u80fd\u627e\u5230\u5339\u914d\u7684\u6587\u6a94\u3002<\/p>\n<h3 id=\"3-add-indexes-TqlUwbFOqP\">3. \u6dfb\u52a0\u7d22\u5f15<\/h3>\n<p>\u5982\u679c\u60a8\u5728\u4e00\u500b\u7f3a\u5c11\u7d22\u5f15\u7684\u5b57\u6bb5\u4e0a\u57f7\u884c\u67e5\u8a62\uff0c\u8003\u616e\u6dfb\u52a0\u7d22\u5f15\u4ee5\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002\u7d22\u5f15\u4f7f MongoDB \u80fd\u5920\u5feb\u901f\u5b9a\u4f4d\u5339\u914d\u7684\u6587\u6a94\uff0c\u6e1b\u5c11\u9047\u5230 NoMatchingDocument \u932f\u8aa4\u7684\u6a5f\u6703\u3002\u4f7f\u7528 <code>createIndex()<\/code> \u65b9\u6cd5\u70ba\u6240\u9700\u5b57\u6bb5\u6dfb\u52a0\u7d22\u5f15\u3002<\/p>\n<pre><code>db.collection.createIndex({ field: 1 })\r\n<\/code><\/pre>\n<p>\u5c07 <code>collection<\/code> \u66ff\u63db\u70ba\u60a8\u7684\u96c6\u5408\u540d\u7a31\uff0c\u5c07 <code>field<\/code> \u66ff\u63db\u70ba\u60a8\u60f3\u8981\u7d22\u5f15\u7684\u5b57\u6bb5\u3002\u503c <code>1<\/code> \u8868\u793a\u5347\u5e8f\u7d22\u5f15\uff0c\u800c <code>-1<\/code> \u8868\u793a\u964d\u5e8f\u7d22\u5f15\u3002<\/p>\n<h3 id=\"4-handle-errors-gracefully-TqlUwbFOqP\">4. \u59a5\u5584\u8655\u7406\u932f\u8aa4<\/h3>\n<p>\u5728\u57f7\u884c\u67e5\u8a62\u6216\u66f4\u65b0\u6642\uff0c\u59a5\u5584\u8655\u7406\u932f\u8aa4\u81f3\u95dc\u91cd\u8981\u3002\u9664\u4e86\u50c5\u4f9d\u8cf4 NoMatchingDocument \u932f\u8aa4\u5916\uff0c\u9084\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u60a8\u7684\u7de8\u7a0b\u8a9e\u8a00\u6216 MongoDB \u9a45\u52d5\u7a0b\u5e8f\u63d0\u4f9b\u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\u3002\u9019\u6a23\uff0c\u60a8\u53ef\u4ee5\u6355\u7372\u4e26\u8655\u7406\u4efb\u4f55\u767c\u751f\u7684\u932f\u8aa4\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"summary-TqlUwbFOqP\">\u7e3d\u7d50<\/h2>\n<p>\u7576\u67e5\u8a62\u6216\u66f4\u65b0\u64cd\u4f5c\u672a\u80fd\u5728\u6307\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u4efb\u4f55\u5339\u914d\u6587\u6a94\u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 47 &#8211; NoMatchingDocument\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u67e5\u8a62\u689d\u4ef6\u3001\u6aa2\u67e5\u6578\u64da\u4e00\u81f4\u6027\u3001\u6dfb\u52a0\u7d22\u5f15\u4e26\u59a5\u5584\u8655\u7406\u932f\u8aa4\u3002\u5982\u679c\u60a8\u9700\u8981\u53ef\u9760\u4e14\u9ad8\u6027\u80fd\u7684 VPS \u4e3b\u6a5f\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6191\u85c9\u5176\u4e00\u6d41\u7684\u670d\u52d9\uff0c\u60a8\u53ef\u4ee5\u5c08\u6ce8\u65bc\u60a8\u7684 MongoDB \u61c9\u7528\u7a0b\u5e8f\uff0c\u800c\u7121\u9700\u64d4\u5fc3\u57fa\u790e\u8a2d\u65bd\u554f\u984c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21641","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21641"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21641\/revisions"}],"predecessor-version":[{"id":23782,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21641\/revisions\/23782"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}