{"id":5221,"date":"2021-06-14T18:23:28","date_gmt":"2021-06-14T10:23:28","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5221"},"modified":"2021-06-14T18:23:28","modified_gmt":"2021-06-14T10:23:28","slug":"mongo%e9%ab%98%e6%80%a7%e8%83%bd%e6%8f%ad%e7%a7%98%e4%b9%8bobjectid%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5221","title":{"rendered":"Mongo\u9ad8\u6027\u80fd\u63ed\u79d8\u4e4bObjectId\u89e3\u5bc6"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u6709\u7a7a\u95f2\u65f6\u95f4\u7684\u65f6\u5019\uff0c\u770b\u770b Redis\u3001Mongo \u7b49\u7cfb\u7edf\u7684\u8bbe\u8ba1\uff0c\u6536\u83b7\u4f1a\u9887\u4e30\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u5728\u524d\u9762\u7684\u4e00\u7ae0\u8bb2\u89e3\u4e86\uff0cMongoDB \u4e2d ObjectId \u7684\u751f\u6210\u539f\u7406\u3002Mongo \u4e2d\u7684 ObjectId \u8bbe\u8ba1\u7684\u5f88\u7cbe\u5999\uff0c\u957f\u5ea6\u6bd4\u96ea\u82b1\u7b97\u6cd5\u8fd8\u957f\uff0c\u8fd8\u80fd\u4e0d\u5b8c\u5168\u4f9d\u8d56\u4e8e\u65f6\u949f\uff08\u6bd4\u5982\u65f6\u949f\u56de\u62e8\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574 pid \u7684\u5f62\u5f0f\u4fdd\u969c ObjcetId \u4e0d\u91cd\u590d\uff09\u3002\u4f46\u662f\u5b83\u4e5f\u6709\u7f3a\u70b9\uff0c\u5c31\u662f\u957f\u5ea6\u592a\u957f\uff0c\u5360\u7528\u7a7a\u95f4\u6bd4\u96ea\u82b1\u7b97\u6cd5\u8fd8\u5927\uff0c\u5728 Java \u4e2d\u6211\u4eec\u5fc5\u987b\u901a\u8fc7\u5b57\u7b26\u4e32\u6765\u6807\u8bc6\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u731c\u6709\u90e8\u5206\u7f51\u53cb\u53ef\u80fd\u4f1a\u8bf4\uff0c\u96ea\u82b1\u7b97\u6cd5\u6211\u901a\u8fc7 Java \u5c31\u53ef\u4ee5\u5b9e\u73b0\u54c8\uff0c\u5355\u72ec\u4e3a\u4e86\u4e00\u4e2a\u5206\u5e03\u5f0f ID\uff0c\u6211\u653e\u5f03\u96ea\u82b1\uff0c\u5f15\u5165 MongoDB \u5e76\u4e0d\u660e\u667a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u786e\u5b9e\u662f\u8fd9\u6837\u7684\uff0c\u4f46\u662f\u6280\u672f\u67b6\u6784\u5e08\u5728\u6280\u672f\u9009\u578b\u65f6\uff0c\u5f80\u5f80\u8003\u8651\u7684\u662f Mongo \u7684 NoSQL \u7279\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mongo \u5728\u8bbe\u8ba1\u4e4b\u521d\uff0c\u5c31\u8003\u8651\u4e86\u5206\u5e03\u5f0f\uff0c\u9ad8\u6027\u80fd\u7684\u63d2\u5165\u7b49\u9700\u6c42\u573a\u666f\uff0c\u6240\u4ee5\u5728 ObjectId \u7684\u8bbe\u8ba1\u4e0a\uff0c\u5b83\u8fd8\u652f\u6301\u8ba9\u5ba2\u6237\u7aef\u751f\u6210 ObjectId\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u7684\u751f\u6210\u65b9\u6cd5\uff0c\u6211\u4eec\u901a\u8fc7 Mongo \u7684 Java \u9a71\u52a8\u7a0b\u5e8f\u6765\u4e00\u63a2\u7a76\u7adf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bbf\u95ee\u7f51\u5740<code>https:\/\/github.com\/mongodb\/mongo-java-driver\/downloads<\/code>\uff0c\u4e0b\u8f7d mongodb \u7684 java driver \u6e90\u7801\uff08\u672c\u5730\u53cd\u7f16\u8bd1\u9a71\u52a8\u7a0b\u5e8f\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u6216\u8005\u76f4\u63a5\u5728 github \u4e0a\u770b\u5bf9\u5e94\u7c7b\u7684\u6e90\u7801\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9a71\u52a8\u6e90\u7801\u6253\u5f00\u540e\uff0c\u6211\u4eec\u627e\u5230 org.bson \u5305\u4e0b\u7684 ObjectId.java \u6587\u4ef6\uff0c\u9488\u5bf9\u6027\u7684\u770b\u770b\u8fd9\u4e2a\u7c7b\uff0c\u8fdb\u884c\u5206\u6790\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u6784\u5efa\u7684 objectId \u4ee3\u7801\u4e3b\u8981\u7531 _time\uff0c_machine \u548c _inc \u7ec4\u6210\u3002\u4e3a\u4e86\u65b9\u4fbf\u8bb2\u89e3\uff0c\u6211\u628a\u90e8\u5206\u6e90\u7801\u6458\u5f55\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ObjectId implements Comparable&lt;ObjectId&gt;, Serializable {\n\tfinal int _time;\n\tfinal int _machine;\n\tfinal int _inc;\n\tboolean _new;\n\n\tpublic ObjectId(){\n\t\t_time = (int) (System.currentTimeMillis() \/ 1000);\n\t\t_machine = _genmachine;\n\t\t_inc = _nextInc.getAndIncrement();\n\t\t_new = true;\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u8fd9\u91cc\u7684 _machine \u53d8\u91cf\uff0c\u5b9e\u9645\u4e0a\u5305\u62ec\u4e86\u673a\u5668\u7801 machinePiece \u548c\u8fdb\u7a0b\u7801 processPiece \u4e24\u4e2a\u90e8\u5206\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">_machine \u53d8\u91cf\u7684\u7b97\u6cd5\u8fc7\u7a0b\u5927\u81f4\u662f\u8fd9\u6837\u7684\uff1a\u5148\u901a\u8fc7 NetworkInterface \u8fd9\u4e2a\u7c7b\uff0c\u83b7\u53d6\u673a\u5668\u7684\u6240\u6709\u7f51\u7edc\u63a5\u53e3\u4fe1\u606f\uff0c\u7136\u540e\u5c06\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u53d6\u6563\u5217\u503c\uff0c\u5c31\u5f97\u5230\u4e86\u673a\u5668\u7801\uff1b\u518d\u7136\u540e\u901a\u8fc7 RuntimeMXBean.getName() \u65b9\u6cd5\u83b7\u53d6 pid\uff0c\u63a5\u7740\u518d\u62fc\u88c5 classloaderid\uff0c\u5f97\u5230\u8fdb\u7a0b\u7801\uff1b\u6700\u540e\u5c06\u673a\u5668\u7801\u548c\u8fdb\u7a0b\u7801\u8fdb\u884c\u4f4d\u6216\u8fd0\u7b97\u5f97\u5230 _machine\u3002\u5f53\u7136\u8fd9\u91cc\u751f\u6210\u7684 _machine \u662f\u5341\u8fdb\u5236\u7684\uff0c\u9700\u8f6c\u6210\u5341\u516d\u8fdb\u5236\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">_time \u5c31\u662f\u6211\u4eec\u7684\u65f6\u95f4\u6233\u53d8\u91cf\uff0c\u76f4\u63a5\u7531 System.currentTimeMillis() \/ 1000 \u8ba1\u7b97\u5f97\u51fa\u7684\u65f6\u95f4\u6233\uff0c\u5f53\u7136\u8fd9\u91cc\u751f\u6210\u7684\u4e5f\u662f 10 \u8fdb\u5236\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">_inc \u53d8\u91cf\u5c31\u662f\u6211\u4eec\u7684\u81ea\u589e\u53d8\u91cf\uff0c\u5b83\u7684\u81ea\u589e\u6570\u662f\u901a\u8fc7 Java \u4e2d\u7684 AtomicInteger \u7c7b\u7684 getAndIncrement() \u65b9\u6cd5\u83b7\u53d6\uff0c\u5b83\u80fd\u4fdd\u8bc1\u6bcf\u6b21\u5f97\u5230\u7684\u503c\u662f\u4e00\u4e2a\u9012\u589e\u5e76\u4e0d\u91cd\u590d\u7684\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7684\u7248\u672c\uff0cObjectId \u6e90\u7801\u53ef\u80fd\u4e0d\u4e00\u6837\uff0c\u6bd4\u5982\u6211\u53c8\u770b\u4e86\u4e00\u4e2a mongo-java-driver-3.8.2.jar\uff0c\u5b83\u5176\u4e2d\u7684\u53d8\u91cf\u5206\u5e03\u5982\u4e0b\uff0c\u4f46\u662f\u6838\u5fc3\u903b\u8f91\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public final class ObjectId implements Comparable&lt;ObjectId&gt;, Serializable {\n    private static final long serialVersionUID = 3670079982654483072L;\n    \/\/ ...\n    private static final AtomicInteger NEXT_COUNTER = new AtomicInteger((new SecureRandom()).nextInt());\n    private final int timestamp;\n    private final int machineIdentifier;\n    private final short processIdentifier;\n    private final int counter;\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u770b\u770b\u673a\u5668\u7801\u7684\u751f\u6210\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static int createMachineIdentifier() {\n    int machinePiece;\n    try {\n        StringBuilder sb = new StringBuilder();\n        Enumeration e = NetworkInterface.getNetworkInterfaces();\n\n        while(e.hasMoreElements()) {\n            NetworkInterface ni = (NetworkInterface)e.nextElement();\n            sb.append(ni.toString());\n            byte[] mac = ni.getHardwareAddress();\n            if (mac != null) {\n                ByteBuffer bb = ByteBuffer.wrap(mac);\n\n                try {\n                    sb.append(bb.getChar());\n                    sb.append(bb.getChar());\n                    sb.append(bb.getChar());\n                } catch (BufferUnderflowException var7) {\n                    ;\n                }\n            }\n        }\n\n        machinePiece = sb.toString().hashCode();\n    } catch (Throwable var8) {\n        machinePiece = (new SecureRandom()).nextInt();\n        LOGGER.debug(\"Failed to get machine identifier from network interface, using SecureRandom instead\");\n    }\n\n    machinePiece &amp;= 16777215;\n    return machinePiece;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u673a\u5668\u7801\u751f\u6210\u7684\u903b\u8f91\u548c\u6211\u4e0a\u9762\u8bf4\u7684 _machine \u7c7b\u4f3c\uff0c\u4e0b\u9762\u518d\u770b\u770b\u8fdb\u7a0b pid \u7684\u83b7\u53d6\u65b9\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static short createProcessIdentifier() {\n    short processId;\n    try {\n        String processName = ManagementFactory.getRuntimeMXBean().getName();\n        if (processName.contains(\"@\")) {\n            processId = (short)Integer.parseInt(processName.substring(0, processName.indexOf(64)));\n        } else {\n            processId = (short)ManagementFactory.getRuntimeMXBean().getName().hashCode();\n        }\n    } catch (Throwable var2) {\n        processId = (short)(new SecureRandom()).nextInt();\n        LOGGER.debug(\"Failed to get process identifier from JMX, using SecureRandom instead\");\n    }\n\n    return processId;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">nodejs \u7b49\u5176\u4ed6\u8bed\u8a00\u7684\u76f8\u5173\u5b9e\u73b0\u4e5f\u662f\u7c7b\u4f3c\u7684\uff0c\u6211\u5c31\u4e0d\u5728\u4e00\u4e00\u5217\u4e3e\u4e86\u3002\u603b\u4e4b Mongo \u7684\u9ad8\u6027\u80fd\u7cbe\u9ad3\u8fd8\u662f\u5f88\u667a\u6167\u7684\uff01<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/06\/mongot.png\" alt=\"\" \/><figcaption>nodejs\u4e2dmongo ObjectID\u7684\u5b9e\u73b0<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136 MongoDB \u4e2d ObjectId \u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u4f46\u662f\u5982\u679c\u5168\u90e8\u5728\u670d\u52a1\u7aef\u751f\u6210\u80af\u5b9a\u4f1a\u82b1\u8d39\u4e00\u70b9\u5f00\u9500\uff0c\u6240\u4ee5 Mongo \u56e2\u961f\u5c31\u60f3\u529e\u6cd5\u8ba9 ObjectId \u4ea4\u7ed9\u5ba2\u6237\u7aef\u53bb\u751f\u6210\uff0c\u8ba9\u540e\u670d\u52a1\u7aef\u5224\u65ad\uff0c\u5982\u679c\u63d2\u5165\u7684\u6570\u636e\u4e2d\u5df2\u7ecf\u6709\u4e86 ObjcetId\uff0c\u6211\u5c31\u91c7\u7528\uff0c\u5982\u679c\u6ca1\u6709 ObjectId\uff0c\u670d\u52a1\u7aef\u518d\u751f\u6210\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mongo \u7684\u9ad8\u6027\u80fd\u4fdd\u969c\uff0cObjectId \u7684\u7cbe\u5999\u8bbe\u8ba1\u53ea\u662f\u5176\u4e2d\u4e4b\u4e00\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5221\">Mongo\u9ad8\u6027\u80fd\u63ed\u79d8\u4e4bObjectId\u89e3\u5bc6<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u6709\u7a7a\u95f2\u65f6\u95f4\u7684\u65f6\u5019\uff0c\u770b\u770b Redis\u3001Mongo \u7b49\u7cfb\u7edf\u7684\u8bbe\u8ba1\uff0c\u6536\u83b7\u4f1a\u9887\u4e30\u3002 \u6211\u5728\u524d\u9762\u7684\u4e00\u7ae0\u8bb2\u89e3\u4e86\uff0cMong [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1774,1767,2046,1578,1775],"class_list":["post-5221","post","type-post","status-publish","format-standard","hentry","category-java","tag-mongoclient","tag-mongocollection","tag-mongod","tag-mongodb","tag-mongoexception"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5221"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5221\/revisions"}],"predecessor-version":[{"id":5222,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5221\/revisions\/5222"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}