{"id":2508,"date":"2025-03-09T09:51:21","date_gmt":"2025-03-09T01:51:21","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2508"},"modified":"2025-03-09T14:44:49","modified_gmt":"2025-03-09T06:44:49","slug":"mongodb-%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf%ef%bc%88auto-increment%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/09\/mongodb-%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf%ef%bc%88auto-increment%ef%bc%89\/","title":{"rendered":"MongoDB \u81ea\u52a8\u589e\u957f\uff08Auto Increment\uff09"},"content":{"rendered":"\n<p>MongoDB \u9ed8\u8ba4\u5e76\u4e0d\u652f\u6301\u50cf\u4f20\u7edf\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982 MySQL\u3001PostgreSQL\uff09\u4e2d\u7684 <strong>\u81ea\u589e\u4e3b\u952e\uff08Auto Increment\uff09<\/strong>\uff0c\u5373\u81ea\u52a8\u4e3a\u6bcf\u4e2a\u65b0\u63d2\u5165\u7684\u6587\u6863\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u9012\u589e\u503c\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u6765\u6a21\u62df\u7c7b\u4f3c\u7684\u884c\u4e3a\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 <code>ObjectId<\/code> \u4f5c\u4e3a\u9ed8\u8ba4\u552f\u4e00\u6807\u8bc6\u7b26<\/strong><\/h2>\n\n\n\n<p>MongoDB \u7684 <code>_id<\/code> \u5b57\u6bb5\u9ed8\u8ba4\u4f7f\u7528 <code>ObjectId<\/code> \u7c7b\u578b\u4f5c\u4e3a\u6bcf\u4e2a\u6587\u6863\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002<code>ObjectId<\/code> \u662f\u4e00\u4e2a 12 \u5b57\u8282\u7684\u6807\u8bc6\u7b26\uff0c\u5305\u542b\u4e86\u65f6\u95f4\u6233\u3001\u673a\u5668\u6807\u8bc6\u3001\u8fdb\u7a0b ID \u548c\u968f\u673a\u6570\u7b49\u4fe1\u606f\uff0c\u786e\u4fdd\u4e86\u6bcf\u4e2a\u751f\u6210\u7684 ID \u90fd\u662f\u552f\u4e00\u7684\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u9012\u589e\u7684\u3002<\/p>\n\n\n\n<p>\u5c3d\u7ba1\u5982\u6b64\uff0c<code>ObjectId<\/code> \u53ef\u4ee5\u6709\u6548\u907f\u514d ID \u91cd\u590d\uff0c\u4f46\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u9012\u589e\u7684 ID\uff0c\u8fd8\u662f\u9700\u8981\u91c7\u53d6\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528\u5e8f\u5217\u6a21\u62df\u81ea\u52a8\u589e\u957f<\/strong><\/h2>\n\n\n\n<p>\u4e3a\u4e86\u5b9e\u73b0\u81ea\u589e\u7684\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u989d\u5916\u7684\u96c6\u5408\u6765\u7ef4\u62a4\u81ea\u589e\u5e8f\u5217\u3002\u6bcf\u5f53\u4f60\u63d2\u5165\u4e00\u4e2a\u65b0\u6587\u6863\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8be5\u96c6\u5408\u6765\u83b7\u53d6\u5f53\u524d\u7684\u6700\u5927\u503c\uff0c\u5e76\u5c06\u5176\u9012\u589e\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u57fa\u672c\u601d\u8def\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(1) \u521b\u5efa\u4e00\u4e2a\u7ef4\u62a4\u5e8f\u5217\u7684\u96c6\u5408<\/strong><\/h3>\n\n\n\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u96c6\u5408\u6765\u5b58\u50a8\u81ea\u589e\u7684\u5e8f\u5217\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a <code>counters<\/code> \u96c6\u5408\uff0c\u8be5\u96c6\u5408\u7684\u6587\u6863\u5305\u542b\u6bcf\u4e2a\u96c6\u5408\u7684\u540d\u79f0\u548c\u5f53\u524d\u7684\u5e8f\u5217\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.createCollection(\"counters\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(2) \u63d2\u5165\u521d\u59cb\u5e8f\u5217\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u5728 <code>counters<\/code> \u96c6\u5408\u4e2d\u63d2\u5165\u6587\u6863\uff0c\u7528\u4e8e\u8ddf\u8e2a\u5404\u4e2a\u96c6\u5408\u7684\u81ea\u589e\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.counters.insert({\n  _id: \"userId\",    \/\/ \u81ea\u589e\u5e8f\u5217\u7684\u540d\u79f0\n  seq: 0            \/\/ \u521d\u59cb\u503c\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(3) \u521b\u5efa\u63d2\u5165\u81ea\u589e ID \u7684\u903b\u8f91<\/strong><\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u63d2\u5165\u65b0\u6587\u6863\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>findAndModify<\/code> \u64cd\u4f5c\u6765\u83b7\u53d6\u5f53\u524d\u7684\u81ea\u589e\u503c\uff0c\u5e76\u5c06\u5176\u9012\u589e\u3002\u6bcf\u6b21\u63d2\u5165\u6570\u636e\u65f6\uff0c\u6211\u4eec\u90fd\u80fd\u83b7\u53d6\u5230\u4e00\u4e2a\u552f\u4e00\u7684\u81ea\u589e ID\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.insert({\n  _id: db.counters.findAndModify({\n    query: { _id: \"userId\" },\n    update: { $inc: { seq: 1 } },\n    new: true\n  }).seq,\n  name: \"John Doe\",\n  email: \"johndoe@example.com\"\n});<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u64cd\u4f5c\u4e2d\uff0c<code>findAndModify<\/code> \u4f1a\u67e5\u627e <code>counters<\/code> \u96c6\u5408\u4e2d\u7684\u6587\u6863 <code>_id<\/code> \u4e3a <code>\"userId\"<\/code> \u7684\u6587\u6863\uff0c\u7136\u540e\u9012\u589e <code>seq<\/code> \u5b57\u6bb5\u5e76\u8fd4\u56de\u65b0\u7684\u81ea\u589e\u503c\u3002\u8fd9\u4e2a\u81ea\u589e\u503c\u4f1a\u4f5c\u4e3a\u65b0\u6587\u6863\u7684 <code>_id<\/code> \u63d2\u5165\u5230 <code>users<\/code> \u96c6\u5408\u4e2d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(4) \u793a\u4f8b\u4ee3\u7801<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a 'counters' \u7684\u96c6\u5408\u6765\u7ef4\u62a4\u5e8f\u5217\ndb.createCollection(\"counters\");\n\n\/\/ \u521d\u59cb\u5316\u81ea\u589e\u5e8f\u5217\ndb.counters.insert({\n  _id: \"userId\",    \/\/ \u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u6807\u8bc6\u7b26\u6765\u8ddf\u8e2a\u4e0d\u540c\u7684\u81ea\u589e\u5e8f\u5217\n  seq: 0            \/\/ \u81ea\u589e\u5e8f\u5217\u7684\u521d\u59cb\u503c\n});\n\n\/\/ \u63d2\u5165\u65b0\u6587\u6863\u65f6\u83b7\u53d6\u5e76\u9012\u589e\u81ea\u589e ID\ndb.users.insert({\n  _id: db.counters.findAndModify({\n    query: { _id: \"userId\" },\n    update: { $inc: { seq: 1 } },  \/\/ \u589e\u52a0\u5e8f\u5217\u503c\n    new: true\n  }).seq,\n  name: \"Jane Smith\",\n  email: \"janesmith@example.com\"\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>(5) \u67e5\u8be2\u751f\u6210\u7684\u81ea\u589e ID<\/strong><\/h3>\n\n\n\n<p>\u6bcf\u6b21\u63d2\u5165\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>_id<\/code> \u5b57\u6bb5\u6765\u67e5\u627e\u548c\u9a8c\u8bc1\u81ea\u589e\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.find({}).sort({ _id: 1 });  \/\/ \u6309\u7167 _id \u9012\u589e\u6392\u5e8f<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4f18\u7f3a\u70b9<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4f18\u70b9\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4fdd\u8bc1\u552f\u4e00\u6027<\/strong>\uff1a\u6bcf\u4e2a\u6587\u6863\u7684\u81ea\u589e ID \u90fd\u662f\u552f\u4e00\u7684\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539 <code>counters<\/code> \u96c6\u5408\u4e2d\u7684\u5e8f\u5217\u540d\u79f0\u6765\u4e3a\u4e0d\u540c\u7684\u96c6\u5408\u5b9e\u73b0\u81ea\u589e\u529f\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7f3a\u70b9\uff1a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u56e0\u4e3a\u6bcf\u6b21\u63d2\u5165\u6587\u6863\u65f6\uff0c\u90fd\u9700\u8981\u8bbf\u95ee\u548c\u66f4\u65b0 <code>counters<\/code> \u96c6\u5408\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\uff0c\u7279\u522b\u662f\u5728\u9ad8\u5e76\u53d1\u5199\u5165\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n\n\n\n<li><strong>\u5355\u70b9\u6545\u969c\u95ee\u9898<\/strong>\uff1a\u6240\u6709\u81ea\u589e\u64cd\u4f5c\u4f9d\u8d56\u4e8e <code>counters<\/code> \u96c6\u5408\uff0c\u8fd9\u4e2a\u96c6\u5408\u53ef\u80fd\u6210\u4e3a\u74f6\u9888\u3002<\/li>\n\n\n\n<li><strong>\u9700\u8981\u989d\u5916\u7684\u7ba1\u7406<\/strong>\uff1a\u6bcf\u4e2a\u9700\u8981\u81ea\u589e ID \u7684\u96c6\u5408\u90fd\u9700\u8981\u4f7f\u7528 <code>findAndModify<\/code> \u64cd\u4f5c\uff0c\u589e\u52a0\u4e86\u989d\u5916\u7684\u590d\u6742\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528 MongoDB \u9a71\u52a8\u521b\u5efa\u81ea\u589e ID<\/strong><\/h2>\n\n\n\n<p>\u9664\u4e86\u76f4\u63a5\u5728 MongoDB Shell \u4e2d\u64cd\u4f5c\uff0c\u4f7f\u7528 MongoDB \u9a71\u52a8\uff08\u5982 Python\u3001Node.js\uff09\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u81ea\u589e ID \u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a <strong>Node.js<\/strong> \u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u81ea\u589e ID \u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { MongoClient } = require('mongodb');\n\nasync function createAutoIncrementId() {\n  const client = new MongoClient('mongodb:\/\/localhost:27017');\n  await client.connect();\n  const db = client.db('mydatabase');\n\n  \/\/ \u83b7\u53d6\u81ea\u589e\u5e8f\u5217\u5e76\u66f4\u65b0\n  const result = await db.collection('counters').findOneAndUpdate(\n    { _id: 'userId' },\n    { $inc: { seq: 1 } },\n    { returnDocument: 'after' }\n  );\n\n  \/\/ \u83b7\u53d6\u65b0\u7684\u81ea\u589e ID\n  const newId = result.value.seq;\n\n  \/\/ \u63d2\u5165\u65b0\u6587\u6863\n  await db.collection('users').insertOne({\n    _id: newId,\n    name: 'Alice',\n    email: 'alice@example.com'\n  });\n\n  console.log(`Inserted user with _id: ${newId}`);\n  await client.close();\n}\n\ncreateAutoIncrementId();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u867d\u7136 MongoDB \u672c\u8eab\u4e0d\u652f\u6301\u81ea\u52a8\u589e\u957f\u5b57\u6bb5\uff08\u50cf MySQL \u4e2d\u7684 AUTO_INCREMENT\uff09\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528\u81ea\u589e\u5e8f\u5217\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728 MongoDB \u4e2d\u6a21\u62df\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u5355\u72ec\u7684\u5e8f\u5217\u96c6\u5408\uff0c\u5e76\u4f7f\u7528 <code>findAndModify<\/code> \u9012\u589e\u5e8f\u5217\u503c\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6587\u6863\u751f\u6210\u552f\u4e00\u7684\u81ea\u589e ID\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u987a\u5e8f ID \u7684\u573a\u666f\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u5728\u9ad8\u5e76\u53d1\u65f6\u53ef\u80fd\u4f1a\u5e26\u6765\u6027\u80fd\u95ee\u9898\u3002\u5982\u679c\u6027\u80fd\u662f\u5173\u952e\uff0c\u8003\u8651\u5176\u4ed6\u65b9\u6cd5\u6216\u67b6\u6784\u8bbe\u8ba1\uff08\u5982\u5206\u5e03\u5f0f ID \u751f\u6210\u7cfb\u7edf\uff09\u53ef\u80fd\u66f4\u5408\u9002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u9ed8\u8ba4\u5e76\u4e0d\u652f\u6301\u50cf\u4f20\u7edf\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982 MySQL\u3001PostgreSQL\uff09\u4e2d\u7684 \u81ea\u589e\u4e3b\u952e\uff08Auto [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2531,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-2508","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\/2508","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=2508"}],"version-history":[{"count":3,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2558,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2508\/revisions\/2558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2531"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}