{"id":950190,"date":"2024-12-27T00:28:04","date_gmt":"2024-12-26T16:28:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/950190.html"},"modified":"2024-12-27T00:28:06","modified_gmt":"2024-12-26T16:28:06","slug":"python%e5%a6%82%e4%bd%95%e8%bf%9e%e6%8e%a5mongodb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/950190.html","title":{"rendered":"python\u5982\u4f55\u8fde\u63a5mongodb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084859\/3dcea235-03bd-40e7-a31f-cbd1a270527a.webp\" alt=\"python\u5982\u4f55\u8fde\u63a5mongodb\" \/><\/p>\n<p><p> <strong>Python\u8fde\u63a5MongoDB\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528PyMongo\u5e93\u5b9e\u73b0\u3001PyMongo\u662f\u5b98\u65b9\u63a8\u8350\u7684Python\u9a71\u52a8\u7a0b\u5e8f\u3001\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4fbf\u6377\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u4e0eMongoDB\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002\u4f7f\u7528PyMongo\u8fde\u63a5MongoDB\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5PyMongo\u5e93\u3001\u521b\u5efaMongoDB\u5ba2\u6237\u7aef\u3001\u8fde\u63a5\u5230\u7279\u5b9a\u6570\u636e\u5e93\u548c\u96c6\u5408\u3001\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u70b9\uff0c\u8ba9\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528PyMongo\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5\u4e86PyMongo\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<code>pip install pymongo<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u7684Python\u811a\u672c\u6216\u4ea4\u4e92\u5f0fShell\u4e2d\u5bfc\u5165pymongo\u6a21\u5757\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>MongoClient<\/code>\u7c7b\u6765\u521b\u5efa\u4e00\u4e2aMongoDB\u5ba2\u6237\u7aef\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u5c06\u7528\u4e8e\u8fde\u63a5\u5230MongoDB\u670d\u52a1\u5668\u3002\u8fde\u63a5\u6210\u529f\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8be5\u5ba2\u6237\u7aef\u6765\u8bbf\u95ee\u4e0d\u540c\u7684\u6570\u636e\u5e93\u548c\u96c6\u5408\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001PyMongo\u5e93\u7684\u5b89\u88c5\u4e0e\u914d\u7f6e<\/p>\n<\/p>\n<p><p>PyMongo\u662fPython\u4e0eMongoDB\u4ea4\u4e92\u7684\u5b98\u65b9\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684API\u6765\u7ba1\u7406MongoDB\u6570\u636e\u5e93\u3002\u8981\u4f7f\u7528PyMongo\uff0c\u9996\u5148\u9700\u8981\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5\u5b83\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b89\u88c5PyMongo\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymongo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u7684\u65b9\u6cd5\u662f\u5c1d\u8bd5\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymongo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u9519\u8bef\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efaMongoDB\u5ba2\u6237\u7aef<\/p>\n<\/p>\n<p><p>\u5728\u6210\u529f\u5b89\u88c5PyMongo\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u521b\u5efa\u4e00\u4e2aMongoDB\u5ba2\u6237\u7aef\u3002\u8fd9\u662f\u8fde\u63a5\u5230MongoDB\u670d\u52a1\u5668\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4f7f\u7528<code>MongoClient<\/code>\u7c7b\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5ba2\u6237\u7aef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pymongo import MongoClient<\/p>\n<p>client = MongoClient(&#39;mongodb:\/\/localhost:27017\/&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8fde\u63a5\u5230\u672c\u5730\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\u3002\u5982\u679cMongoDB\u5728\u5176\u4ed6\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\uff0c\u4f60\u9700\u8981\u66ff\u6362URL\u4ee5\u5339\u914d\u76ee\u6807\u670d\u52a1\u5668\u7684\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u5ba2\u6237\u7aef\u6210\u529f\u521b\u5efa\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u8fde\u63a5\u5230\u7279\u5b9a\u7684\u6570\u636e\u5e93\u3002MongoDB\u4e0d\u9700\u8981\u663e\u5f0f\u521b\u5efa\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u5c06\u5728\u9996\u6b21\u5b58\u50a8\u6570\u636e\u65f6\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">db = client[&#39;mydatabase&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>mydatabase<\/code>\u662f\u6211\u4eec\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u540d\u79f0\u3002\u5982\u679c\u8be5\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0cMongoDB\u5c06\u5728\u9996\u6b21\u5199\u5165\u6570\u636e\u65f6\u81ea\u52a8\u521b\u5efa\u5b83\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u64cd\u4f5c\u96c6\u5408<\/p>\n<\/p>\n<p><p>\u96c6\u5408\u662fMongoDB\u4e2d\u7684\u8868\u683c\u7b49\u4ef7\u7269\u3002\u8981\u64cd\u4f5c\u96c6\u5408\uff0c\u4f60\u9700\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u9009\u62e9\u4e00\u4e2a\u96c6\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">collection = db[&#39;mycollection&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>mycollection<\/code>\u662f\u96c6\u5408\u7684\u540d\u79f0\u3002\u5982\u679c\u8be5\u96c6\u5408\u5c1a\u672a\u5b58\u5728\uff0c\u5b83\u5c06\u5728\u9996\u6b21\u5199\u5165\u6570\u636e\u65f6\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u63d2\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>MongoDB\u4e2d\u7684\u6570\u636e\u4ee5\u6587\u6863\u7684\u5f62\u5f0f\u5b58\u50a8\u3002\u6587\u6863\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002\u53ef\u4ee5\u4f7f\u7528<code>insert_one<\/code>\u6216<code>insert_many<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u63d2\u5165\u96c6\u5408\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">document = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p>result = collection.insert_one(document)<\/p>\n<h2><strong>\u63d2\u5165\u591a\u4e2a\u6587\u6863<\/strong><\/h2>\n<p>documents = [<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;San Francisco&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;age&quot;: 35, &quot;city&quot;: &quot;Los Angeles&quot;}<\/p>\n<p>]<\/p>\n<p>result = collection.insert_many(documents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63d2\u5165\u64cd\u4f5c\u8fd4\u56de\u4e00\u4e2a<code>InsertOneResult<\/code>\u6216<code>InsertManyResult<\/code>\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b83\u4eec\u83b7\u53d6\u63d2\u5165\u64cd\u4f5c\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u67e5\u8be2\u6570\u636e<\/p>\n<\/p>\n<p><p>PyMongo\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u67e5\u8be2\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>find_one<\/code>\u6216<code>find<\/code>\u65b9\u6cd5\u6765\u67e5\u8be2\u96c6\u5408\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u627e\u5355\u4e2a\u6587\u6863<\/p>\n<p>query = {&quot;name&quot;: &quot;Alice&quot;}<\/p>\n<p>document = collection.find_one(query)<\/p>\n<h2><strong>\u67e5\u627e\u591a\u4e2a\u6587\u6863<\/strong><\/h2>\n<p>documents = collection.find({&quot;city&quot;: &quot;San Francisco&quot;})<\/p>\n<p>for doc in documents:<\/p>\n<p>    print(doc)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u67e5\u8be2\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\u5bf9\u8c61\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Python\u7684\u5b57\u5178\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u66f4\u65b0\u6570\u636e<\/p>\n<\/p>\n<p><p>\u66f4\u65b0\u6587\u6863\u53ef\u4ee5\u4f7f\u7528<code>update_one<\/code>\u6216<code>update_many<\/code>\u65b9\u6cd5\u3002\u66f4\u65b0\u64cd\u4f5c\u9700\u8981\u4e00\u4e2a\u67e5\u8be2\u53c2\u6570\u6765\u5b9a\u4f4d\u8981\u66f4\u65b0\u7684\u6587\u6863\u548c\u4e00\u4e2a\u66f4\u65b0\u53c2\u6570\u6765\u6307\u5b9a\u8981\u8fdb\u884c\u7684\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u66f4\u65b0\u5355\u4e2a\u6587\u6863<\/p>\n<p>query = {&quot;name&quot;: &quot;Alice&quot;}<\/p>\n<p>new_values = {&quot;$set&quot;: {&quot;age&quot;: 26}}<\/p>\n<p>collection.update_one(query, new_values)<\/p>\n<h2><strong>\u66f4\u65b0\u591a\u4e2a\u6587\u6863<\/strong><\/h2>\n<p>query = {&quot;city&quot;: &quot;San Francisco&quot;}<\/p>\n<p>new_values = {&quot;$set&quot;: {&quot;city&quot;: &quot;SF&quot;}}<\/p>\n<p>collection.update_many(query, new_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<code>UpdateResult<\/code>\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u68c0\u67e5\u4fee\u6539\u7684\u6587\u6863\u6570\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5220\u9664\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5220\u9664\u6587\u6863\u540c\u6837\u4f7f\u7528<code>delete_one<\/code>\u6216<code>delete_many<\/code>\u65b9\u6cd5\u3002\u4f60\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u67e5\u8be2\u6765\u5b9a\u4f4d\u8981\u5220\u9664\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5355\u4e2a\u6587\u6863<\/p>\n<p>query = {&quot;name&quot;: &quot;Alice&quot;}<\/p>\n<p>collection.delete_one(query)<\/p>\n<h2><strong>\u5220\u9664\u591a\u4e2a\u6587\u6863<\/strong><\/h2>\n<p>query = {&quot;city&quot;: &quot;SF&quot;}<\/p>\n<p>collection.delete_many(query)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5220\u9664\u64cd\u4f5c\u8fd4\u56de\u4e00\u4e2a<code>DeleteResult<\/code>\u5bf9\u8c61\uff0c\u5b83\u63d0\u4f9b\u4e86\u6709\u5173\u5220\u9664\u64cd\u4f5c\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5904\u7406\u8fde\u63a5\u548c<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u786e\u4fdd\u6b63\u786e\u5173\u95edMongoDB\u5ba2\u6237\u7aef\u3002\u8fd9\u6709\u52a9\u4e8e\u91ca\u653e\u8d44\u6e90\u5e76\u4fdd\u6301\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">client.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528<code>close()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b89\u5168\u5730\u5173\u95ed\u8fde\u63a5\uff0c\u786e\u4fdd\u4e0d\u4f1a\u7559\u4e0b\u4efb\u4f55\u672a\u91ca\u653e\u7684\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u5f02\u5e38\u5904\u7406\u4e0e\u8c03\u8bd5<\/p>\n<\/p>\n<p><p>\u5728\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u662f\u5f88\u91cd\u8981\u7684\u3002PyMongo\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8c03\u8bd5\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    client = MongoClient(&#39;mongodb:\/\/localhost:27017\/&#39;)<\/p>\n<p>    # \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/p>\n<p>except pymongo.errors.ConnectionError as e:<\/p>\n<p>    print(f&quot;Connection error: {e}&quot;)<\/p>\n<p>except pymongo.errors.PyMongoError as e:<\/p>\n<p>    print(f&quot;MongoDB error: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    client.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4f60\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5404\u79cd\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1aPython\u4e0eMongoDB\u7684\u96c6\u6210\u975e\u5e38\u65b9\u4fbf\uff0c\u901a\u8fc7\u4f7f\u7528PyMongo\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u8fde\u63a5\u548c\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u6570\u636e\u7684\u63d2\u5165\u3001\u67e5\u8be2\u3001\u66f4\u65b0\u8fd8\u662f\u5220\u9664\uff0cPyMongo\u90fd\u63d0\u4f9b\u4e86\u76f4\u89c2\u800c\u5f3a\u5927\u7684API\u3002\u901a\u8fc7\u6709\u6548\u7684\u8d44\u6e90\u7ba1\u7406\u548c\u5f02\u5e38\u5904\u7406\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5MongoDB\u9a71\u52a8\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8fde\u63a5MongoDB\uff0c\u60a8\u9700\u8981\u5b89\u88c5<code>pymongo<\/code>\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u5b89\u88c5\uff1a  <\/p>\n<pre><code>pip install pymongo\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\u5e76\u5f00\u59cb\u4e0eMongoDB\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u8fde\u63a5MongoDB\u65f6\u9700\u8981\u63d0\u4f9b\u54ea\u4e9b\u4fe1\u606f\uff1f<\/strong><br \/>\u5728\u8fde\u63a5MongoDB\u65f6\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684URI\uff0c\u901a\u5e38\u5305\u62ec\u4e3b\u673a\u540d\u3001\u7aef\u53e3\u53f7\u3001\u6570\u636e\u5e93\u540d\u79f0\u4ee5\u53ca\u53ef\u9009\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code>mongodb:\/\/username:password@localhost:27017\/mydatabase\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<code>localhost<\/code>\u662fMongoDB\u670d\u52a1\u5668\u5730\u5740\uff0c<code>27017<\/code>\u662f\u9ed8\u8ba4\u7aef\u53e3\uff0c<code>mydatabase<\/code>\u662f\u60a8\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\u540d\u79f0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0eMongoDB\u8fde\u63a5\u4e2d\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u8fde\u63a5MongoDB\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff0c\u4f8b\u5982\u7f51\u7edc\u95ee\u9898\u6216\u8ba4\u8bc1\u5931\u8d25\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u5e76\u8fdb\u884c\u5904\u7406\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from pymongo import MongoClient\nfrom pymongo.errors import ConnectionF<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lure\n\ntry:\n    client = MongoClient(&#39;mongodb:\/\/localhost:27017\/&#39;)\n    # \u6d4b\u8bd5\u8fde\u63a5\n    client.admin.command(&#39;ping&#39;)\n    print(&quot;\u6210\u529f\u8fde\u63a5\u5230MongoDB&quot;)\nexcept ConnectionFailure:\n    print(&quot;\u65e0\u6cd5\u8fde\u63a5\u5230MongoDB&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u8fde\u63a5\u4e0d\u6210\u529f\u65f6\uff0c\u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fde\u63a5MongoDB\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528PyMongo\u5e93\u5b9e\u73b0\u3001PyMongo\u662f\u5b98\u65b9\u63a8\u8350\u7684Python\u9a71\u52a8\u7a0b\u5e8f [&hellip;]","protected":false},"author":3,"featured_media":950194,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950190"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=950190"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950190\/revisions"}],"predecessor-version":[{"id":950196,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950190\/revisions\/950196"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/950194"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=950190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=950190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=950190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}