{"id":9517,"date":"2023-02-27T16:16:09","date_gmt":"2023-02-27T08:16:09","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9517"},"modified":"2023-02-27T16:30:54","modified_gmt":"2023-02-27T08:30:54","slug":"docker%e5%ae%89%e8%a3%85mongodb%e5%8f%8a%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9517","title":{"rendered":"docker\u5b89\u88c5mongoDB\u53ca\u4f7f\u7528"},"content":{"rendered":"\n<p><strong>\u4e00\u3001mongoDB\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>MongoDB\u662f\u4e00\u4e2aNoSQL\u7684\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93 \uff0c\u652f\u6301\u6d77\u91cf\u6570\u636e\u5b58\u50a8\uff0c\u9ad8\u6027\u80fd\u7684\u8bfb\u5199\u3002<\/p>\n\n\n\n<p><strong>1. mongo\u7684\u4f53\u7cfb\u7ed3\u6784<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-164.png\" alt=\"\" class=\"wp-image-9518\" width=\"233\" height=\"260\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-164.png 510w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-164-268x300.png 268w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-164-230x257.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-164-350x391.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-164-480x536.png 480w\" sizes=\"(max-width: 233px) 100vw, 233px\" \/><\/figure><\/div>\n\n\n\n<ul><li>mongo\u4e2d\u7684<mark>\u96c6\u5408<\/mark>\u76f8\u5f53\u4e8emysql\u4e2d<mark>\u8868<\/mark>\u7684\u6982\u5ff5\uff1b<\/li><li>mongo\u4e2d\u7684<mark>\u6587\u6863<\/mark>\u76f8\u5f53\u4e8emysql\u4e2d<mark>\u884c<\/mark>\u7684\u6982\u5ff5\uff1b<\/li><li>mongo\u4e2d\u7684<mark>\u57df<\/mark>\u76f8\u5f53\u4e8emysql\u4e2d<mark>\u5b57\u6bb5\/\u5217<\/mark>\u7684\u6982\u5ff5\uff1b<\/li><\/ul>\n\n\n\n<p><strong>2. mongoDB\u7684\u7279\u70b9(\u6216\u4f7f\u7528\u573a\u666f)<\/strong><\/p>\n\n\n\n<ul><li>\u652f\u6301\u5b58\u50a8\u6d77\u91cf\u6570\u636e\uff1b(\u4f8b\u5982\uff1a\u76f4\u64ad\u4e2d\u7684\u6253\u8d4f\u6570\u636e)\uff1b<\/li><li>\u652f\u6301\u9891\u7e41\u7684\u6570\u636e\u8bfb\u5199\uff1b\uff08\u4f8b\u5982\uff1a\u6e38\u620f\u9053\u5177\uff09\uff1b<\/li><li>\u6570\u636e\u5b89\u5168\u6027\u4e0d\u9ad8\uff0c\u5b58\u5728\u6570\u636e\u8bef\u5dee(\u4e22\u5931\u6570\u636e)\uff1b<\/li><li>mongoDB\u4e0d\u652f\u6301\u591a\u8868\u64cd\u4f5c\uff0c\u4e0d\u652f\u6301\u4e8b\u52a1\uff1b<\/li><li>mongoDB\u4f7f\u7528Bson\u5b58\u50a8\u683c\u5f0f\uff0c\u652f\u6301\u52a8\u6001\u5b57\u6bb5\u7ba1\u7406\uff1b<\/li><\/ul>\n\n\n\n<p><strong>3. mongoDB\u4e0emysql\u3001redis\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<p>\u4e0eredis\u5bf9\u6bd4<\/p>\n\n\n\n<ul><li>redis\u7eaf\u5185\u5b58\u6570\u636e\u5e93\uff0c\u5185\u5b58\u4e0d\u8db3\u65f6\u89e6\u53d1\u6dd8\u6c70\u7b56\u7565\uff0cmongoDB\u4f7f\u7528\u5185\u5b58\u52a0\u78c1\u76d8\u7684\u5b58\u50a8\u7b56\u7565\u5177\u6709\u9ad8\u6269\u5c55\u6027\uff1b<\/li><li>mongoDB\u4f7f\u7528Bson\u5b58\u50a8\u683c\u5f0f\uff0c\u652f\u6301\u52a8\u6001\u5b57\u6bb5\u7ba1\u7406\u65b9\u4fbf\u6269\u5c55\uff1b<\/li><\/ul>\n\n\n\n<p>\u4e0emysql\u5bf9\u6bd4<\/p>\n\n\n\n<ul><li>mongoDB\u4e0d\u652f\u6301\u591a\u8868\u64cd\u4f5c\uff0c\u4e0d\u652f\u6301\u4e8b\u52a1\uff1b<\/li><li>mongoDB\u4f7f\u7528Bson\u5b58\u50a8\u683c\u5f0f\uff0c\u652f\u6301\u52a8\u6001\u5b57\u6bb5\u7ba1\u7406\uff1b<\/li><\/ul>\n\n\n\n<p>\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4<\/p>\n\n\n\n<ul><li>Redis &gt; MongoDB &gt; MySQL<\/li><\/ul>\n\n\n\n<p><strong>4. mongoDB\u5b58\u50a8\u539f\u7406<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-165-1024x620.png\" alt=\"\" class=\"wp-image-9519\" width=\"470\" height=\"284\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-1024x620.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-300x182.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-768x465.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-1536x930.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-830x503.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-230x139.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-350x212.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165-480x291.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-165.png 1582w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n\n<ul><li>mongoDb\u91c7\u7528\u5185\u5b58\u52a0\u78c1\u76d8\u7684\u65b9\u5f0f\u5b58\u50a8\u6570\u636e\uff1b<\/li><li>mongoDb\u652f\u6301\u6570\u636e\u5206\u7247\uff0c\u5f53\u5355\u4e00\u7684\u670d\u52a1\u5668\u4e2d\u78c1\u76d8\u4e0d\u591f\u7528\u7684\u65f6\u5019\uff0c\u8fd8\u53ef\u4ee5\u4e32\u8054\u5176\u4ed6\u670d\u52a1\u5668\uff1b<\/li><li>\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u5230\u8fbe\u5185\u5b58\u65f6\uff0c\u5148\u5728\u65e5\u5fd7\u4e2d\u8bb0\u5f55\u4e0b\u64cd\u4f5c\u8bb0\u5f55\uff0c\u7136\u540e\u518d\u53bb\u64cd\u4f5c\u5185\u5b58\uff1b<\/li><li>\u5185\u5b58\u4e2d\u7684\u65e5\u5fd7\u6bcf10ms\u5411\u78c1\u76d8\u4e2d\u7684\u65e5\u5fd7\u8fdb\u884c\u540c\u6b65\u4e00\u6b21\uff0c\u6570\u636e\u5219\u6bcf\u5206\u949f\u540c\u6b65\u4e00\u6b21\uff1b<\/li><li>\u5ba2\u6237\u7aef\u5148\u53bb\u5185\u5b58\u4e2d\u67e5\u8be2\u6570\u636e\uff0c\u5185\u5b58\u4e2d\u6ca1\u6709\u518d\u53bb\u67e5\u8be2\u78c1\u76d8\uff1b<\/li><li>\u5f53\u5ba2\u6237\u7aef\u5199\u5165\u7684\u65f6\u5019\uff0c\u4f1a\u5148\u5199\u5165\u5230\u5185\u5b58\u4e2d\uff0c\u5185\u5b58\u4e2d\u5199\u5165\u540e\u8bf7\u6c42\u76f4\u63a5\u8fd4\u56de\uff0c\u5185\u5b58\u4e2d\u7684\u6570\u636e\u4f1a\u6839\u636e\u540c\u6b65\u7b56\u7565\u540c\u6b65\u5230\u78c1\u76d8\uff1b<\/li><li>\u5982\u679c\u673a\u5668\u5b95\u673a\uff0c\u5728\u91cd\u542f\u670d\u52a1\u7684\u65f6\u5019\u4f1a\u89e3\u6790\u78c1\u76d8\u4e2d\u7684\u65e5\u5fd7\u548c\u78c1\u76d8\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5bf9\u6bd4\uff0c\u5c06\u672a\u5165\u5230\u78c1\u76d8\u4e2d\u7684\u6570\u636e\u5199\u5165\u78c1\u76d8\uff0c\u4f46\u53ef\u80fd\u4f1a\u4e22\u593110ms\u7684\u6570\u636e\uff1b<\/li><\/ul>\n\n\n\n<p><strong>\u4e8c\u3001\u4f7f\u7528docker\u5b89\u88c5mongo<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.\u5b89\u88c5<\/strong><\/p>\n\n\n\n<ol><li>\u62c9\u53d6mongo\u955c\u50cf<\/li><\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> docker pull mongo:4.4<\/pre>\n\n\n\n<p>2.\u521b\u5efamongo\u6570\u636e\u6301\u4e45\u5316\u76ee\u5f55<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir -p \/docker_volume\/mongodb\/data<\/pre>\n\n\n\n<p>3.\u8fd0\u884c\u5bb9\u5668<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">docker run -itd --name mongo -v \/docker_volume\/mongodb\/data:\/data\/db -p 27017:27017 mongo:4.4 --auth<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\n-v: \u5c06\u5bbf\u4e3b\u673a\u7684\/docker_volume\/mongodb\/data\u6620\u5c04\u5230\u5bb9\u5668\u7684\/data\/db\u76ee\u5f55\uff0c\u5c06\u6570\u636e\u6301\u4e45\u5316\u5230\u5bbf\u4e3b\u673a\uff0c\u4ee5\u9632\u6b62\u5220\u9664\u5bb9\u5668\u540e\uff0c\u5bb9\u5668\u5185\u7684\u6570\u636e\u4e22\u5931\n\u2013auth\uff1a\u9700\u8981\u5bc6\u7801\u624d\u80fd\u8bbf\u95ee\u5bb9\u5668\u670d\u52a1<\/pre>\n\n\n\n<p><strong>2.\u521b\u5efa\u7528\u6237<\/strong><\/p>\n\n\n\n<p>1.\u767b\u5f55mongo\u5bb9\u5668\uff0c\u5e76\u8fdb\u5165\u5230\u3010admin\u3011\u6570\u636e\u5e93<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> docker exec -it mongo mongo admin<\/pre>\n\n\n\n<p>2.\u521b\u5efa\u4e00\u4e2a\u7528\u6237\uff0cmongo \u9ed8\u8ba4\u6ca1\u6709\u7528\u6237<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},'readWriteAnyDatabase']});<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u3010user:\u2018root\u2019 \u3011\uff1a\u8bbe\u7f6e\u7528\u6237\u540d\u4e3aroot\n\u3010pwd:\u2018123456\u2019\u3011\uff1a\u8bbe\u7f6e\u5bc6\u7801\u4e3a123456\n\u3010role:\u2018userAdminAnyDatabase\u2019\u3011\uff1a\u53ea\u5728admin\u6570\u636e\u5e93\u4e2d\u53ef\u7528\uff0c\u8d4b\u4e88\u7528\u6237\u6240\u6709\u6570\u636e\u5e93\u7684userAdmin\u6743\u9650\n\u3010db: \u2018admin\u2019\u3011\uff1a\u53ef\u64cd\u4f5c\u7684\u6570\u636e\u5e93\n\u3010\u2018readWriteAnyDatabase\u2019\u3011\uff1a\u8d4b\u4e88\u7528\u6237\u8bfb\u5199\u6743\u9650\n<\/pre>\n\n\n\n<p>dbAdmin\uff1a\u5141\u8bb8\u7528\u6237\u5728\u6307\u5b9a\u6570\u636e\u5e93\u4e2d\u6267\u884c\u7ba1\u7406\u51fd\u6570\uff0c\u5982\u7d22\u5f15\u521b\u5efa\u3001\u5220\u9664\uff0c\u67e5\u770b\u7edf\u8ba1\u6216\u8bbf\u95eesystem.profile<\/p>\n\n\n\n<p><strong>3. \u8fde\u63a5\u3001\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>1.\u8fde\u63a5mongo\u6570\u636e\u5e93<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">db.auth('root', '123456')<\/pre>\n\n\n\n<p>2.\u6d4b\u8bd5\u6570\u636e\u5e93\uff0c\u63d2\u5165\u4e00\u6761\u8bed\u53e5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> db.user.insert({\"name\":\"zhangsan\",\"age\":18})<\/pre>\n\n\n\n<p>3.\u6d4b\u8bd5\u6570\u636e\u5e93\uff0c\u67e5\u8be2\u521a\u624d\u63d2\u5165\u7684\u8bed\u53e5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> db.user.find()<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-166-1024x235.png\" alt=\"\" class=\"wp-image-9520\" width=\"516\" height=\"118\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-1024x235.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-300x69.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-768x176.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-1536x353.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-830x191.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-230x53.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-350x80.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166-480x110.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-166.png 1942w\" sizes=\"(max-width: 516px) 100vw, 516px\" \/><\/figure><\/div>\n\n\n\n<p>4.navicat\u8fde\u63a5\u6d4b\u8bd5<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-167-1024x716.png\" alt=\"\" class=\"wp-image-9521\" width=\"467\" height=\"326\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-1024x716.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-300x210.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-768x537.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-830x580.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-230x161.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-350x245.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167-480x335.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-167.png 1342w\" sizes=\"(max-width: 467px) 100vw, 467px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u4e09\u3001SpringBoot\u6574\u5408mongoDB<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.\u5bfc\u5165\u5750\u6807<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        &lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-data-mongodb&lt;\/artifactId>\n             &lt;version>2.3.9.RELEASE&lt;\/version>\n        &lt;\/dependency>\n<\/pre>\n\n\n\n<p><strong>2.\u6dfb\u52a0yml\u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring:\n  data:\n    mongodb:\n      uri: mongodb:\/\/192.156.136.168:27017\/testdb\n      username: root\n      password: 123456<\/pre>\n\n\n\n<p><strong>3.\u7f16\u5199\u5b9e\u4f53\u7c7b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import lombok.AllArgsConstructor;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\nimport org.bson.types.ObjectId;\nimport org.springframework.data.annotation.Id;\nimport org.springframework.data.mongodb.core.mapping.Document;\nimport org.springframework.data.mongodb.core.mapping.Field;\n\n@Data\n@AllArgsConstructor\n@NoArgsConstructor\n\/\/\u6307\u5b9a\u5b9e\u4f53\u7c7b\u548c\u6570\u636e\u5e93\u6587\u6863\u7684\u6620\u5c04\u5173\u7cfb    \u9ed8\u8ba4\u5b9e\u4f53\u7c7b\u540d  \u6570\u636e\u5e93\u5982\u679c\u6ca1\u6709\u8be5\u6587\u6863\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\n@Document(value=\"tb_person\")\npublic class Person {\n    @Id\n    private ObjectId id; \/\/mongoDB\u63a8\u8350\u4f7f\u7528ID\n    \/\/\u6307\u5b9a\u5c5e\u6027\u540d\u548c\u6570\u636e\u5e93\u57df\u7684\u6620\u5c04\u5173\u7cfb   \u9ed8\u8ba4\u5c5e\u6027\u540d\n    @Field(\"person_name\")\n    private String name;\n    private int age;\n    private String address;\n}\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">1.@Document(value=\u201ctb_person\u201d) \uff1a\u6307\u5b9a\u5b9e\u4f53\u7c7b\u548c\u6570\u636e\u5e93\u6587\u6863\u7684\u6620\u5c04\u5173\u7cfb \u9ed8\u8ba4\u5b9e\u4f53\u7c7b\u540d \u6570\u636e\u5e93\u5982\u679c\u6ca1\u6709\u8be5\u6587\u6863\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\n2. @Field(\u201cperson_name\u201d)\uff1a \/\/\u6307\u5b9a\u5c5e\u6027\u540d\u548c\u6570\u636e\u5e93\u57df\u7684\u6620\u5c04\u5173\u7cfb \u9ed8\u8ba4\u5c5e\u6027\u540d<\/pre>\n\n\n\n<p><strong>4.\u6d4b\u8bd5\u7c7b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.junit.Test;\nimport org.junit.runner.RunWith;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.data.domain.Sort;\nimport org.springframework.data.mongodb.core.MongoTemplate;\nimport org.springframework.data.mongodb.core.query.Criteria;\nimport org.springframework.data.mongodb.core.query.Query;\nimport org.springframework.data.mongodb.core.query.Update;\nimport org.springframework.test.context.junit4.SpringRunner;\n  \n    \/**\n     * \u591a\u6761\u4ef6\u67e5\u8be2\n     *\/\n    @Test\n    public void find() {\n        \/\/\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6 age\u5c0f\u4e8e30,\u4e14person_name=\"\u5f20\u4e09\"\n        Criteria criteria = Criteria.where(\"age\").lt(30)\n                .and(\"person_name\").is(\"\u5f20\u4e09\");\n        \n        \/\/\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6\n        Query query = new Query(criteria);\n        \/\/\u67e5\u8be2\n        List&lt;Person> list = mongoTemplate.find(query, Person.class);\n\n        for (Person person : list) {\n            System.out.println(person);\n        }\n    }\n\n    \/**\n     * \u5206\u9875\u67e5\u8be2\n     *\/\n    @Test\n    public void findPage() {\n        \/\/\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6 age\u5c0f\u4e8e30,\u4e14person_name=\"\u5f20\u4e09\"\n        Criteria criteria = Criteria.where(\"age\").lt(30)\n                .and(\"person_name\").is(\"\u5f20\u4e09\");\n\n        \/\/\u6839\u636e\u6761\u4ef6 \u67e5\u8be2\u603b\u6570\n        Query queryCount = new Query(criteria);\n        long count = mongoTemplate.count(queryCount, Person.class);\n\n\n        \/\/\u67e5\u8be2\u5f53\u524d\u9875\u7684\u6570\u636e\u5217\u8868, \u67e5\u8be2\u7b2c\u4e8c\u9875\uff0c\u6bcf\u9875\u67e5\u8be22\u6761\n        Query queryLimit = new Query(criteria)\n                .with(Sort.by(Sort.Order.desc(\"age\")))\n                .limit(2)\/\/\u6bcf\u9875\u67e5\u8be2\u6761\u6570\n                .skip(2); \/\/\u4ece\u7b2c\u51e0\u9875\u5f00\u59cb \uff08page-1\uff09*size\n\n        List&lt;Person> list = mongoTemplate.find(queryLimit, Person.class);\n        for (Person person : list) {\n            System.out.println(person);\n        }\n    }\n    \n    \/**\n     * \u66f4\u65b0\u6570\u636e\n     *\/\n    @Test\n    public void update() {\n        \/\/\u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6 age\u5c0f\u4e8e30,\u4e14person_name=\"\u5f20\u4e09\"\n        Criteria criteria = Criteria.where(\"person_name\").is(\"\u738b\u4e94\");\n        \/\/\u8bbe\u7f6e\u66f4\u65b0\u6761\u4ef6\n        Query query = new Query(criteria);\n        \/\/\u8bbe\u7f6e\u66f4\u65b0\u6570\u636e\n        Update update = new Update();\n        update.set(\"age\", 16);\n        mongoTemplate.upsert(query, update, Person.class);\n    }\n\n    \/**\n     * \u4fdd\u5b58\n     *\/\n    @Test\n    public void save() {\n        Person person = new Person();\n        person.setName(\"\u5f20\u4e09\");\n        person.setAge(18);\n        mongoTemplate.save(person);\n    }\n    \n    \/**\n     * \u5220\u9664\u6570\u636e\n     *\/\n    @Test\n    public void dlete() {\n        mongoTemplate.remove(Query.query(Criteria.where(\"person_name\").is(\"\u5f20\u4e09\")), Person.class);\n    }\n<\/pre>\n\n\n\n<p><strong>5.mongoDB\u7d22\u5f15<\/strong><\/p>\n\n\n\n<p><code>\u63d0\u793a\uff1a1 \uff1a\u5347\u5e8f\u7d22\u5f15 -1 \uff1a\u964d\u5e8f\u7d22\u5f15<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#\u67e5\u770b\u7d22\u5f15\ndb.user.getIndexes()\n#\u521b\u5efa\u7d22\u5f15\n#db.user.createIndex({'age':1})\n<\/pre>\n\n\n\n<p><strong>\u56db\u3001mongoDB\u539f\u751f\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u65b0\u589e<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">db.tb_person.insert({person_name: \"\u9648\u516d\", age: 16})<\/pre>\n\n\n\n<p><strong>\u4fee\u6539<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- \u666e\u901a\u4fee\u6539\ndb.tb_person.update({age: 16}, {$set: {person_name: \"\u5f20\u4e09\"}})<\/pre>\n\n\n\n<p><code>\u4fee\u6539\u683c\u5f0f\uff1a<\/code><br><code>db.collection.update(query, update, [ upsert: boolean, multi: boolean, writeConcern: document])<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">db.tb_person.update({age: 16}, {$set: {person_name: \"\u5f20\u4e09\"}}, {upsert: true, multi: true})\n\n\t--  upsert \uff08\u9ed8\u8ba4false \uff09 : \u53ef\u9009\uff0c\u5982\u679c\u4e0d\u5b58\u5728update\u7684\u8bb0\u5f55\uff0c\u662f\u5426\u4fdd\u5b58\u3002true\u4e3a\u4fdd\u5b58\u3002\n\t--  multi\uff08\u9ed8\u8ba4false \uff09 : \u53ef\u9009\uff0c\u9ed8\u8ba4\u53ea\u66f4\u65b0\u7b2c\u4e00\u6761\u8bb0\u5f55\u3002true:\u66f4\u65b0\u6240\u6709\u5339\u914d\u6570\u636e\n\t--  writeConcern :\u53ef\u9009\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\n<\/pre>\n\n\n\n<p><strong>\u5220\u9664<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- \u666e\u901a\u5220\u9664\ndb.tb_person.remove({person_name: \"\u5f20\u4e09\"})\n<\/pre>\n\n\n\n<p><code>\u5220\u9664\u683c\u5f0f\uff1a<\/code><br><code>db.collection.remove(query, update, [ justOne: boolean, writeConcern: document])<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">db.tb_person.remove({person_name: \"\u5f20\u4e09\"}, false)\n\n\t-- justOne: \uff08\u53ef\u9009\uff09true\uff1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u8bb0\u5f55\uff0cfalse\uff1a\u5220\u9664\u6240\u6709\n\t-- writeConcern :\uff08\u53ef\u9009\uff09\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\n<\/pre>\n\n\n\n<p><strong>\u67e5\u8be2<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">--  \u67e5\u8be2person_name=\u5f20\u4e09 \u6216\u8005 \u5e74\u9f84 18\ndb.tb_person.find({$or:[{person_name: \"\u5f20\u4e09\"},{age:18 }]}) \n\n-- \u5206\u9875\u67e5\u8be2  \u4ee5\u5e74\u9f84\u5347\u5e8f\u6392\u5e8f \u8df3\u8fc7\u7b2c1\u6761\u6570\u636e\uff0c\u67e5\u8be2\u540e\u97622\u6761\u6570\u636e\ndb.tb_person.find().sort({age:1}).limit(2).skip(1)  \n\n-- \u67e5\u8be2\u5e74\u9f84\u5c0f\u4e8e\u7b49\u4e8e21\u7684\u6570\u636e\uff0c\u4ee5\u5e74\u9f84\u964d\u5e8f\u6392\u5e8f\ndb.tb_person.find({age:{$lte:21}}).sort({age:-1}) \n<\/pre>\n\n\n\n<p><strong>\u5e38\u7528\u67e5\u8be2\u6761\u4ef6<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-168-1024x274.png\" alt=\"\" class=\"wp-image-9523\" width=\"555\" height=\"148\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-1024x274.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-300x80.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-768x205.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-1536x411.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-830x222.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-230x61.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-350x94.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168-480x128.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-168.png 1960w\" sizes=\"(max-width: 555px) 100vw, 555px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001mongoDB\u662f\u4ec0\u4e48\uff1f MongoDB\u662f\u4e00\u4e2aNoSQL\u7684\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93 \uff0c\u652f\u6301\u6d77\u91cf\u6570\u636e\u5b58\u50a8\uff0c\u9ad8\u6027\u80fd\u7684\u8bfb\u5199\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27,23],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9517"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9517"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9517\/revisions"}],"predecessor-version":[{"id":9525,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9517\/revisions\/9525"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}