{"id":948171,"date":"2024-12-27T00:01:14","date_gmt":"2024-12-26T16:01:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948171.html"},"modified":"2024-12-27T00:01:15","modified_gmt":"2024-12-26T16:01:15","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96bson","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/948171.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6bson"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083626\/628d1271-f0f2-4bd7-8853-0b48666e19d2.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6bson\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6BSON\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>bson<\/code>\u6a21\u5757\u548c<code>pymongo<\/code>\u5e93\u3001\u901a\u8fc7\u53cd\u5e8f\u5217\u5316\u51fd\u6570\u8fdb\u884c\u6570\u636e\u89e3\u6790\u3001\u7406\u89e3BSON\u683c\u5f0f\u7684\u7ed3\u6784\u3002<\/strong>\u901a\u8fc7\u4f7f\u7528<code>bson<\/code>\u6a21\u5757\u53ef\u4ee5\u76f4\u63a5\u89e3\u6790BSON\u6570\u636e\uff0c<code>pymongo<\/code>\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u4e0eMongoDB\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u65b9\u5f0f\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u8bfb\u53d6BSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>bson<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u81ea\u5e26\u7684\u6807\u51c6\u5e93\u4e0d\u652f\u6301BSON\u683c\u5f0f\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<code>bson<\/code>\u6765\u5b9e\u73b0BSON\u6570\u636e\u7684\u89e3\u6790\u3002BSON\uff08Binary JSON\uff09\u662f\u4e00\u79cd\u4e8c\u8fdb\u5236\u7f16\u7801\u7684JSON\u6587\u6863\u683c\u5f0f\uff0c\u4e3b\u8981\u7528\u4e8eMongoDB\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5bson\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86<code>bson<\/code>\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install bson<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6BSON\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>bson<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u89e3\u6790BSON\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>bson.loads()<\/code>\u6765\u89e3\u6790BSON\u6570\u636e\u6d41\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bson<\/p>\n<p>def read_bson_data(bson_data):<\/p>\n<p>    document = bson.loads(bson_data)<\/p>\n<p>    return document<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>bson_data = bson.BSON.encode({&#39;name&#39;: &#39;John Doe&#39;, &#39;age&#39;: 30})<\/p>\n<p>document = read_bson_data(bson_data)<\/p>\n<p>print(document)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u4e00\u4e2aPython\u5b57\u5178\u7f16\u7801\u4e3aBSON\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>bson.loads()<\/code>\u51fd\u6570\u89e3\u7801\u56dePython\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>pymongo<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>pymongo<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u4e0eMongoDB\u6570\u636e\u5e93\u4ea4\u4e92\u7684Python\u5e93\uff0c\u5176\u4e2d\u4e5f\u5305\u542b\u4e86BSON\u5904\u7406\u7684\u529f\u80fd\u3002\u901a\u8fc7<code>pymongo<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bfb\u53d6\u5b58\u50a8\u5728MongoDB\u4e2d\u7684BSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5pymongo<\/h4>\n<\/p>\n<p><p>\u4e0e<code>bson<\/code>\u6a21\u5757\u7c7b\u4f3c\uff0c<code>pymongo<\/code>\u4e5f\u9700\u8981\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pymongo<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fde\u63a5MongoDB\u5e76\u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pymongo<\/code>\u8fde\u63a5\u5230MongoDB\u6570\u636e\u5e93\uff0c\u5e76\u8bfb\u53d6\u96c6\u5408\u4e2d\u7684BSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pymongo import MongoClient<\/p>\n<p>def fetch_documents_from_mongodb(uri, db_name, collection_name):<\/p>\n<p>    client = MongoClient(uri)<\/p>\n<p>    db = client[db_name]<\/p>\n<p>    collection = db[collection_name]<\/p>\n<p>    documents = collection.find()<\/p>\n<p>    result = []<\/p>\n<p>    for document in documents:<\/p>\n<p>        result.append(document)<\/p>\n<p>    client.close()<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>uri = &quot;mongodb:\/\/localhost:27017\/&quot;<\/p>\n<p>db_name = &quot;test_db&quot;<\/p>\n<p>collection_name = &quot;test_collection&quot;<\/p>\n<p>documents = fetch_documents_from_mongodb(uri, db_name, collection_name)<\/p>\n<p>for doc in documents:<\/p>\n<p>    print(doc)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8fde\u63a5\u5230\u672c\u5730MongoDB\u5b9e\u4f8b\u5e76\u8bfb\u53d6\u6307\u5b9a\u6570\u636e\u5e93\u548c\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7406\u89e3BSON\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>BSON\u683c\u5f0f\u662fMongoDB\u7684\u9ed8\u8ba4\u5b58\u50a8\u683c\u5f0f\uff0c\u5b83\u4e0eJSON\u7c7b\u4f3c\uff0c\u4f46\u652f\u6301\u66f4\u591a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u65e5\u671f\u3001\u4e8c\u8fdb\u5236\u6570\u636e\u548cObjectID\u7b49\u3002\u7406\u89e3BSON\u683c\u5f0f\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u5904\u7406MongoDB\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001BSON\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>BSON\u6587\u6863\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u952e\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u4e32<\/strong>\uff1aUTF-8\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<li><strong>\u6574\u6570<\/strong>\uff1a32\u4f4d\u621664\u4f4d\u6574\u6570\u3002<\/li>\n<li><strong>\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/strong>\uff1aIEEE 754\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002<\/li>\n<li><strong>\u5e03\u5c14\u503c<\/strong>\uff1atrue\u6216false\u3002<\/li>\n<li><strong>\u65e5\u671f<\/strong>\uff1a\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u6beb\u79d2\u6570\u3002<\/li>\n<li><strong>ObjectID<\/strong>\uff1a\u4e00\u4e2a12\u5b57\u8282\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<\/li>\n<li><strong>\u6570\u7ec4<\/strong>\uff1a\u503c\u7684\u6709\u5e8f\u96c6\u5408\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001BSON\u4e0eJSON\u7684\u533a\u522b<\/h4>\n<\/p>\n<p><p>\u867d\u7136BSON\u4e0eJSON\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e8c\u8fdb\u5236\u683c\u5f0f<\/strong>\uff1aBSON\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u89e3\u6790\u901f\u5ea6\u66f4\u5feb\uff0c\u9002\u5408\u5b58\u50a8\u548c\u4f20\u8f93\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b\u652f\u6301<\/strong>\uff1aBSON\u652f\u6301\u66f4\u591a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u65e5\u671f\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/li>\n<li><strong>\u5927\u5c0f\u4f18\u5316<\/strong>\uff1aBSON\u683c\u5f0f\u7684\u6587\u6863\u901a\u5e38\u6bd4JSON\u66f4\u5927\uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86\u7c7b\u578b\u4fe1\u606f\u548c\u4e00\u4e9b\u989d\u5916\u7684\u5143\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001BSON\u6570\u636e\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>BSON\u901a\u5e38\u7528\u4e8eMongoDB\u4e2d\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6\u9700\u8981\u9ad8\u6548\u5b58\u50a8\u548c\u4f20\u8f93\u6570\u636e\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001MongoDB\u4e2d\u7684\u5e94\u7528<\/h4>\n<\/p>\n<p><p>MongoDB\u4f7f\u7528BSON\u683c\u5f0f\u5b58\u50a8\u6570\u636e\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002\u6b64\u5916\uff0cBSON\u8fd8\u652f\u6301MongoDB\u72ec\u6709\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982ObjectID\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7f51\u7edc\u6570\u636e\u4f20\u8f93<\/h4>\n<\/p>\n<p><p>\u7531\u4e8eBSON\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u56e0\u6b64\u5b83\u5728\u7f51\u7edc\u4f20\u8f93\u4e2d\u6bd4JSON\u66f4\u9ad8\u6548\u3002\u5bf9\u4e8e\u9700\u8981\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u5927\u91cf\u6570\u636e\u7684\u5e94\u7528\uff0cBSON\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u5907\u4efd\u548c\u6062\u590d<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5907\u4efd\u548c\u6062\u590d\u4e2d\u4f7f\u7528BSON\u683c\u5f0f\uff0c\u53ef\u4ee5\u4fdd\u6301\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u7c7b\u578b\u4fe1\u606f\uff0c\u907f\u514d\u56e0\u683c\u5f0f\u4e0d\u517c\u5bb9\u5bfc\u81f4\u7684\u6570\u636e\u4e22\u5931\u6216\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406BSON\u6570\u636e\u7684\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406BSON\u6570\u636e\u65f6\uff0c\u6709\u4e00\u4e9b\u6280\u5de7\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6BSON\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5BSON\u6570\u636e\u7684\u957f\u5ea6\u548c\u683c\u5f0f\u6765\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u636e\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\uff0c\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002<code>pymongo<\/code>\u5e93\u63d0\u4f9b\u4e86\u6279\u91cf\u63d2\u5165\u548c\u66f4\u65b0\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u6b21\u6570\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u9519\u8bef\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406BSON\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u6bd4\u5982\u6570\u636e\u683c\u5f0f\u9519\u8bef\u3001\u8fde\u63a5\u5931\u8d25\u7b49\u3002\u5efa\u8bae\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u903b\u8f91\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5e94\u5bf9\u8fd9\u4e9b\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u4f18\u5316\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u5728MongoDB\u4e2d\uff0c\u5408\u7406\u5730\u4f7f\u7528\u7d22\u5f15\u53ef\u4ee5\u52a0\u5feb\u6570\u636e\u67e5\u8be2\u7684\u901f\u5ea6\u3002\u786e\u4fdd\u5bf9\u5e38\u7528\u7684\u67e5\u8be2\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u8bfb\u53d6\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001BSON\u4e0e\u5176\u4ed6\u6570\u636e\u683c\u5f0f\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u9664\u4e86BSON\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5982JSON\u3001XML\u548cYAML\u3002\u4e86\u89e3\u8fd9\u4e9b\u683c\u5f0f\u4e4b\u95f4\u7684\u533a\u522b\uff0c\u6709\u52a9\u4e8e\u9009\u62e9\u6700\u5408\u9002\u7684\u6570\u636e\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001BSON vs JSON<\/h4>\n<\/p>\n<ul>\n<li>BSON\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0cJSON\u662f\u6587\u672c\u683c\u5f0f\u3002<\/li>\n<li>BSON\u652f\u6301\u66f4\u591a\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>BSON\u901a\u5e38\u6bd4JSON\u66f4\u5927\uff0c\u4f46\u89e3\u6790\u901f\u5ea6\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001BSON vs XML<\/h4>\n<\/p>\n<ul>\n<li>BSON\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0cXML\u662f\u6587\u672c\u683c\u5f0f\u3002<\/li>\n<li>BSON\u6bd4XML\u66f4\u7d27\u51d1\uff0c\u89e3\u6790\u901f\u5ea6\u66f4\u5feb\u3002<\/li>\n<li>XML\u652f\u6301\u590d\u6742\u7684\u7ed3\u6784\u548c\u6807\u7b7e\uff0c\u800cBSON\u66f4\u9002\u5408\u7b80\u5355\u7684\u952e\u503c\u5bf9\u3002<\/li>\n<\/ul>\n<p><h4>3\u3001BSON vs YAML<\/h4>\n<\/p>\n<ul>\n<li>BSON\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0cYAML\u662f\u6587\u672c\u683c\u5f0f\u3002<\/li>\n<li>BSON\u89e3\u6790\u901f\u5ea6\u66f4\u5feb\uff0c\u4f46YAML\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u8f91\u3002<\/li>\n<li>YAML\u9002\u5408\u914d\u7f6e\u6587\u4ef6\uff0cBSON\u9002\u5408\u9ad8\u6548\u5b58\u50a8\u548c\u4f20\u8f93\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001BSON\u5728\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u652f\u6301<\/h3>\n<\/p>\n<p><p>BSON\u4e0d\u4ec5\u5728Python\u4e2d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u8fd8\u5728\u5176\u4ed6\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5f97\u5230\u4e86\u652f\u6301\u3002\u4e86\u89e3\u8fd9\u4e9b\u8bed\u8a00\u4e2d\u7684BSON\u652f\u6301\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u5e73\u53f0\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><h4>1\u3001JavaScript\u4e2d\u7684BSON<\/h4>\n<\/p>\n<p><p>JavaScript\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>bson<\/code>\u6a21\u5757\u6765\u5904\u7406BSON\u6570\u636e\u3002Node.js\u73af\u5883\u4e0b\uff0c<code>bson<\/code>\u6a21\u5757\u975e\u5e38\u6d41\u884c\uff0c\u7528\u4e8e\u4e0eMongoDB\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const BSON = require(&#39;bson&#39;);<\/p>\n<p>const bson = new BSON();<\/p>\n<p>const obj = { name: &#39;Alice&#39;, age: 25 };<\/p>\n<p>const bsonData = bson.serialize(obj);<\/p>\n<p>const deserializedObj = bson.deserialize(bsonData);<\/p>\n<p>console.log(deserializedObj);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Java\u4e2d\u7684BSON<\/h4>\n<\/p>\n<p><p>Java\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>org.bson<\/code>\u5305\u6765\u5904\u7406BSON\u6570\u636e\u3002MongoDB\u63d0\u4f9b\u4e86\u5b98\u65b9\u7684Java\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5bf9BSON\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import org.bson.BsonDocument;<\/p>\n<p>import org.bson.BsonInt32;<\/p>\n<p>import org.bson.BsonString;<\/p>\n<p>public class BsonExample {<\/p>\n<p>    public static void m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(String[] args) {<\/p>\n<p>        BsonDocument document = new BsonDocument();<\/p>\n<p>        document.append(&quot;name&quot;, new BsonString(&quot;Bob&quot;));<\/p>\n<p>        document.append(&quot;age&quot;, new BsonInt32(28));<\/p>\n<p>        System.out.println(document.toJson());<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684BSON<\/h4>\n<\/p>\n<p><p>\u9664\u4e86JavaScript\u548cJava\uff0cBSON\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e5f\u5f97\u5230\u4e86\u652f\u6301\uff0c\u5982C#\u3001C++\u3001Go\u3001Ruby\u7b49\u3002\u8fd9\u4e9b\u8bed\u8a00\u901a\u5e38\u90fd\u6709\u5b98\u65b9\u6216\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684BSON\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u89e3\u6790\u548c\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f18\u5316BSON\u6570\u636e\u5904\u7406\u7684\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528BSON\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u4f18\u5316\u7b56\u7565\u6765\u63d0\u9ad8\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u91cf<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u5b57\u6bb5\uff0c\u53ef\u4ee5\u51cf\u5c11BSON\u6570\u636e\u7684\u5927\u5c0f\uff0c\u4ece\u800c\u63d0\u9ad8\u4f20\u8f93\u6548\u7387\u3002\u907f\u514d\u5728\u6587\u6863\u4e2d\u5b58\u50a8\u8fc7\u591a\u7684\u5d4c\u5957\u7ed3\u6784\u548c\u4e0d\u5fc5\u8981\u7684\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u538b\u7f29\u6280\u672f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u7684BSON\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u538b\u7f29\u6280\u672f\uff0c\u5982gzip\u6216zlib\uff0c\u4ee5\u51cf\u5c11\u6570\u636e\u7684\u5927\u5c0f\u548c\u4f20\u8f93\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b9a\u671f\u8fdb\u884c\u6570\u636e\u6e05\u7406<\/h4>\n<\/p>\n<p><p>\u5728MongoDB\u4e2d\uff0c\u5b9a\u671f\u6e05\u7406\u4e0d\u518d\u9700\u8981\u7684\u6570\u636e\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5e93\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528TTL\u7d22\u5f15\u6216\u5b9a\u671f\u8fd0\u884c\u6e05\u7406\u811a\u672c\u6765\u5220\u9664\u8fc7\u671f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u76d1\u63a7\u6570\u636e\u5e93\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528MongoDB\u81ea\u5e26\u7684\u76d1\u63a7\u5de5\u5177\u6216\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u6570\u636e\u5e93\u7684\u6027\u80fd\u74f6\u9888\u548c\u4f18\u5316\u65b9\u5411\u3002\u901a\u8fc7\u76d1\u63a7\u67e5\u8be2\u6027\u80fd\u3001\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5\u7b49\u6307\u6807\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u4f18\u5316BSON\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u5728Python\u4e2d\u8bfb\u53d6\u548c\u5904\u7406BSON\u6570\u636e\uff0c\u5e76\u5e94\u7528\u4e8e\u5404\u79cd\u573a\u666f\u4e2d\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>bson<\/code>\u6a21\u5757\u8fd8\u662f<code>pymongo<\/code>\u5e93\uff0c\u7406\u89e3BSON\u683c\u5f0f\u548cMongoDB\u7684\u4f7f\u7528\u90fd\u662f\u5173\u952e\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u4e3a\u60a8\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u6307\u5bfc\u548c\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5bson\u6a21\u5757\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8bfb\u53d6bson\u6587\u4ef6\uff0c\u60a8\u9700\u8981\u9996\u5148\u5b89\u88c5pymongo\u5e93\uff0c\u5b83\u5305\u542b\u4e86bson\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<code>pip install pymongo<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165bson\u6a21\u5757\u5e76\u4f7f\u7528\u76f8\u5173\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bfb\u53d6bson\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\uff1f<\/strong><br \/>\u4f7f\u7528pymongo\u7684bson\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6bson\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u6253\u5f00\u6587\u4ef6\u5e76\u4f7f\u7528<code>bson.decode_all()<\/code>\u51fd\u6570\u5c06\u5176\u5185\u5bb9\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import bson\n\nwith open(&#39;your_file.bson&#39;, &#39;rb&#39;) as file:\n    data = bson.decode_all(file.read())\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06bson\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6570\u636e\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u5178\u5217\u8868\u3002<\/p>\n<p><strong>\u8bfb\u53d6bson\u6570\u636e\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6bson\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u6bd4\u5982\u6587\u4ef6\u8def\u5f84\u4e0d\u6b63\u786e\u6216\u6587\u4ef6\u683c\u5f0f\u4e0d\u5339\u914d\u3002\u786e\u4fdd\u60a8\u63d0\u4f9b\u7684\u6587\u4ef6\u8def\u5f84\u662f\u51c6\u786e\u7684\uff0c\u5e76\u4e14\u6587\u4ef6\u786e\u5b9e\u662fbson\u683c\u5f0f\u3002\u5982\u679c\u9047\u5230\u89e3\u7801\u9519\u8bef\uff0c\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u635f\u574f\uff0c\u6216\u8005\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u5de5\u5177\u6253\u5f00\u548c\u9a8c\u8bc1bson\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6BSON\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528bson\u6a21\u5757\u548cpymongo\u5e93\u3001\u901a\u8fc7\u53cd\u5e8f\u5217\u5316\u51fd\u6570\u8fdb\u884c\u6570\u636e\u89e3\u6790\u3001\u7406\u89e3BSO [&hellip;]","protected":false},"author":3,"featured_media":948177,"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\/948171"}],"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=948171"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948171\/revisions"}],"predecessor-version":[{"id":948178,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948171\/revisions\/948178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948177"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}