{"id":38429,"date":"2025-05-20T13:02:28","date_gmt":"2025-05-20T05:02:28","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/38429.html"},"modified":"2025-05-20T13:02:28","modified_gmt":"2025-05-20T05:02:28","slug":"mongodb-%e5%ba%94%e7%94%a8%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/38429.html","title":{"rendered":"MongoDB \u5e94\u7528\u5b9e\u6218"},"content":{"rendered":"<h2>1. java \u539f\u751f\u5ba2\u6237\u7aef<\/h2>\n<p>\u5f15\u5165maven<\/p>\n<p>1 &lt;dependencies&gt;<br \/>\n2 &lt;dependency&gt;<br \/>\n3 &lt;groupId&gt;org.mongodb&lt;\/groupId&gt;<br \/>\n4 &lt;artifactId&gt;mongodb\u2010driver\u2010sync&lt;\/artifactId&gt;<br \/>\n5 &lt;version&gt;4.1.1&lt;\/version&gt;<br \/>\n6 &lt;\/dependency&gt;<br \/>\n7 &lt;\/dependencies&gt;<br \/>\n1 public class QuickStart {<br \/>\n2<br \/>\n3 public static void main(String[] args) {<br \/>\n4 \/\/ \u8fde\u63a5\u672c\u5730\u9ed8\u8ba4\u7aef\u53e3\u7684Mongod<br \/>\n5 \/\/ MongoClient mongoClient &#061; MongoClients.create()<br \/>\n6 \/\/ \u8fde\u63a5\u8fdc\u7a0b\u670d\u52a1\u7684\u6307\u5b9a\u7aef\u53e3\u7684Mongod<br \/>\n7 \/\/ MongoClient mongoClient &#061; MongoClients.create(&#034;mongodb:\/\/host1:27017&#034;);<br \/>\n8 \/\/ \u8fde\u63a5\u6307\u5b9a\u7aef\u53e3\u590d\u5236\u96c6<br \/>\n9 \/\/ MongoClient mongoClient &#061; MongoClients.create(&#034;mongodb:\/\/host1:27017,host<br \/>\n2:27017,host3:27017\/?replicaSet&#061;myReplicaSet&#034;);<br \/>\n10 \/\/ \u8fde\u63a5 mongos\u8def\u7531: \u8fde\u63a5\u4e00\u4e2a<br \/>\n11 \/\/ MongoClient mongoClient &#061; MongoClients.create( &#034;mongodb:\/\/localhost:27017&#034;<br \/>\n);<br \/>\n12 \/\/ \u8fde\u63a5\u591a\u4e2amongos\u8def\u7531<br \/>\n13 MongoClient mongoClient &#061;<br \/>\nMongoClients.create(&#034;mongodb:\/\/111.229.189.98:4000&#034;);<br \/>\n14<br \/>\n15<br \/>\n16 \/\/\u83b7\u53d6\u6570\u636e\u5e93<br \/>\n17 MongoDatabase database &#061; mongoClient.getDatabase(&#034;productdb&#034;);<br \/>\n18<br \/>\n19 \/\/ \u83b7\u53d6\u96c6\u5408<br \/>\n20 MongoCollection&lt;Document&gt; productdesc&#061;database.getCollection( &#034;productdesc&#034;<br \/>\n);<br \/>\n21<br \/>\n22 Document doc &#061; new Document(&#034;name&#034;, &#034;MongoDB&#034;)<br \/>\n23 .append(&#034;type&#034;, &#034;database&#034;)<br \/>\n24 .append(&#034;count&#034;, 1)<br \/>\n25 .append(&#034;versions&#034;, Arrays.asList(&#034;v3.2&#034;, &#034;v3.0&#034;, &#034;v2.6&#034;))<br \/>\n26 .append(&#034;info&#034;, new Document(&#034;x&#034;, 203).append(&#034;y&#034;, 102));<br \/>\n27<br \/>\n28 productdesc.insertOne(doc);<br \/>\n29<br \/>\n30<br \/>\n31 Bson eq &#061; eq(&#034;name&#034;, &#034;MongoDB&#034;);<br \/>\n32 FindIterable&lt;Document&gt; find &#061; productdesc.find(eq);<br \/>\n33 Document first&#061;find.first();<br \/>\n34 System.out.println(first);<br \/>\n35<br \/>\n36 }<br \/>\n37 } <\/p>\n<h2>2. Spring Boot \u6574\u5408&#xff1a;<\/h2>\n<h3>1. \u5f15\u5165maven<\/h3>\n<p>1 &lt;?xml version&#061;&#034;1.0&#034; encoding&#061;&#034;UTF\u20108&#034;?&gt;<br \/>\n2 &lt;project xmlns&#061;&#034;http:\/\/maven.apache.org\/POM\/4.0.0&#034; xmlns:xsi&#061;&#034;http:\/\/www.w3.or<br \/>\ng\/2001\/XMLSchema\u2010instance&#034;<br \/>\n3 xsi:schemaLocation&#061;&#034;http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.or<br \/>\ng\/xsd\/maven\u20104.0.0.xsd&#034;&gt;<br \/>\n4 &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;<br \/>\n5 &lt;parent&gt;<br \/>\n6 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n7 &lt;artifactId&gt;spring\u2010boot\u2010starter\u2010parent&lt;\/artifactId&gt;<br \/>\n8 &lt;version&gt;2.4.0&lt;\/version&gt;<br \/>\n9 &lt;relativePath\/&gt; &lt;!\u2010\u2010 lookup parent from repository \u2010\u2010&gt;<br \/>\n10 &lt;\/parent&gt;<br \/>\n11 &lt;groupId&gt;com.example&lt;\/groupId&gt;<br \/>\n12 &lt;artifactId&gt;demo&lt;\/artifactId&gt;<br \/>\n13 &lt;version&gt;0.0.1\u2010SNAPSHOT&lt;\/version&gt;<br \/>\n14 &lt;name&gt;demo&lt;\/name&gt;<br \/>\n15 &lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;<br \/>\n16<br \/>\n17 &lt;properties&gt;<br \/>\n18 &lt;java.version&gt;1.8&lt;\/java.version&gt;<br \/>\n19 &lt;\/properties&gt;<br \/>\n20<br \/>\n21 &lt;dependencies&gt;<br \/>\n22 &lt;dependency&gt;<br \/>\n23 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n24 &lt;artifactId&gt;spring\u2010boot\u2010starter\u2010data\u2010mongodb&lt;\/artifactId&gt;<br \/>\n25 &lt;\/dependency&gt;<br \/>\n26 &lt;dependency&gt;<br \/>\n27 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n28 &lt;artifactId&gt;spring\u2010boot\u2010starter\u2010web&lt;\/artifactId&gt;<br \/>\n29 &lt;\/dependency&gt;<br \/>\n30<br \/>\n31 &lt;dependency&gt;<br \/>\n32 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n33 &lt;artifactId&gt;spring\u2010boot\u2010starter\u2010test&lt;\/artifactId&gt;<br \/>\n34 &lt;scope&gt;test&lt;\/scope&gt;<br \/>\n35 &lt;\/dependency&gt;<br \/>\n36 &lt;dependency&gt;<br \/>\n37 &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;<br \/>\n38 &lt;artifactId&gt;lombok&lt;\/artifactId&gt;<br \/>\n39 &lt;version&gt;1.18.12&lt;\/version&gt;<br \/>\n40 &lt;\/dependency&gt;<br \/>\n41<br \/>\n42<br \/>\n43 &lt;\/dependencies&gt;<br \/>\n44<br \/>\n45 &lt;build&gt;<br \/>\n46 &lt;plugins&gt;<br \/>\n47 &lt;plugin&gt;<br \/>\n48 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n49 &lt;artifactId&gt;spring\u2010boot\u2010maven\u2010plugin&lt;\/artifactId&gt;<br \/>\n50 &lt;\/plugin&gt;<br \/>\n51 &lt;\/plugins&gt;<br \/>\n52 &lt;\/build&gt;<br \/>\n53<br \/>\n54 &lt;\/project&gt; <\/p>\n<h3>2. \u6dfb\u52a0\u914d\u7f6e\u7c7b \u6bd4\u5982\u8fde\u63a5\u4e0a\u8ff0\u7684 mongos, \u4f7f\u7528MongoTemplate \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c<\/h3>\n<p> package com.example.demo.config;<br \/>\n2<br \/>\n3 import com.mongodb.client.MongoClient;<br \/>\n4 import com.mongodb.client.MongoClients;<br \/>\n5 import org.springframework.context.annotation.Bean;<br \/>\n6 import org.springframework.context.annotation.Configuration;<br \/>\n7 import org.springframework.data.mongodb.core.MongoTemplate;<br \/>\n8<br \/>\n9 &#064;Configuration<br \/>\n10 public class AppConfig {<br \/>\n11<br \/>\n12 public &#064;Bean<br \/>\n13 MongoClient mongoClient() {<br \/>\n14 return MongoClients.create(&#034;mongodb:\/\/111.229.189.98:4000&#034;);<br \/>\n15 }<br \/>\n16<br \/>\n17 public &#064;Bean<br \/>\n18 MongoTemplate mongoTemplate() {<br \/>\n19 return new MongoTemplate(mongoClient(), &#034;productdb&#034;);<br \/>\n20 }<br \/>\n21 } <\/p>\n<h3>3. \u6d4b\u8bd5\u7c7b<\/h3>\n<p>1 package com.example.demo;<br \/>\n2<br \/>\n3 public class Person {<br \/>\n4<br \/>\n5 private String id;<br \/>\n6 private String name;<br \/>\n7 private int age;<br \/>\n8<br \/>\n9 public Person(String name, int age) {<br \/>\n10 this.name &#061; name;<br \/>\n11 this.age &#061; age;<br \/>\n12 }<br \/>\n13<br \/>\n14 public String getId() {<br \/>\n15 return id;<br \/>\n16 }<br \/>\n17 public String getName() {<br \/>\n18 return name;<br \/>\n19 }<br \/>\n20 public int getAge() {<br \/>\n21 return age;<br \/>\n22 }<br \/>\n23<br \/>\n24 &#064;Override<br \/>\n25 public String toString() {<br \/>\n26 return &#034;Person [id&#061;&#034; &#043; id &#043; &#034;, name&#061;&#034; &#043; name &#043; &#034;, age&#061;&#034; &#043; age &#043; &#034;]&#034;;<br \/>\n27 }<br \/>\n28<br \/>\n29 }<br \/>\n30<br \/>\n31 package com.example.demo;<br \/>\n32<br \/>\n33 import lombok.extern.slf4j.Slf4j;<br \/>\n34 import org.springframework.beans.factory.annotation.Autowired;<br \/>\n35 import org.springframework.boot.ApplicationArguments;<br \/>\n36 import org.springframework.boot.ApplicationRunner;<br \/>\n37 import org.springframework.data.mongodb.core.MongoTemplate;<br \/>\n38 import org.springframework.stereotype.Component;<br \/>\n39<br \/>\n40 import java.util.List;<br \/>\n41<br \/>\n42 import static org.springframework.data.mongodb.core.query.Criteria.where;<br \/>\n43 import static org.springframework.data.mongodb.core.query.Query.query;<br \/>\n44 import static org.springframework.data.mongodb.core.query.Update.update;<br \/>\n45<br \/>\n46 &#064;Component<br \/>\n47 &#064;Slf4j<br \/>\n48 public class ApplicationRunnerTest implements ApplicationRunner{<br \/>\n49<br \/>\n50 &#064;Autowired<br \/>\n51 private MongoTemplate mongoOps;<br \/>\n52<br \/>\n53 &#064;Override<br \/>\n54 public void run(ApplicationArguments applicationArguments) throws Exception {<br \/>\n55<br \/>\n56 Person p &#061; new Person(&#034;Joe&#034;, 34);<br \/>\n57<br \/>\n58 \/\/ \u63d2\u5165\u6587\u6863<br \/>\n59 mongoOps.insert(p);<br \/>\n60 log.info(&#034;Insert: &#034; &#043; p);<br \/>\n61<br \/>\n62 \/\/ \u67e5\u8be2\u6587\u6863<br \/>\n63 p &#061; mongoOps.findById(p.getId(), Person.class);<br \/>\n64 log.info(&#034;Found: &#034; &#043; p);<br \/>\n65<br \/>\n66 \/\/ \u66f4\u65b0\u6587\u6863<br \/>\n67 mongoOps.updateFirst(query(where(&#034;name&#034;).is(&#034;Joe&#034;)), update(&#034;age&#034;, 35), Perso<br \/>\nn.class);<br \/>\n68 p &#061; mongoOps.findOne(query(where(&#034;name&#034;).is(&#034;Joe&#034;)), Person.class);<br \/>\n69 log.info(&#034;Updated: &#034; &#043; p);<br \/>\n70<br \/>\n71 \/\/ \u5220\u9664\u6587\u6863<br \/>\n72 mongoOps.remove(p);<br \/>\n73<br \/>\n74 \/\/ Check that deletion worked<br \/>\n75 List&lt;Person&gt; people &#061; mongoOps.findAll(Person.class);<br \/>\n76 log.info(&#034;Number of people &#061; : &#034; &#043; people.size());<br \/>\n77 mongoOps.dropCollection(Person.class);<br \/>\n78 }<br \/>\n79 }<br \/>\n80<br \/>\n81<br \/>\n82<br \/>\n83 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb283\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Java\u9879\u76ee\u4e2d\u901a\u8fc7Maven\u5f15\u5165MongoDB\u9a71\u52a8\uff0c\u5e76\u5c55\u793a\u4e86\u539f\u751fJava\u5ba2\u6237\u7aef\u548cSpring Boot\u6574\u5408MongoDB\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u5728\u539f\u751fJava\u5ba2\u6237\u7aef\u4e2d\uff0c\u901a\u8fc7MongoClients.create()\u65b9\u6cd5\u8fde\u63a5MongoDB\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u67e5\u8be2\u548c\u66f4\u65b0\u6587\u6863\u3002\u5728Spring Boot\u9879\u76ee\u4e2d\uff0c\u901a\u8fc7\u914d\u7f6e\u7c7bAppConfig\u5b9a\u4e49MongoClient\u548cMongoTemplate\uff0c\u5e76\u4f7f\u7528MongoTemplate\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u6d4b\u8bd5\u7c7bApplicationRunnerTest\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528M_mogodb client csdn<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[733,3368,323,190,100,427],"topic":[],"class_list":["post-38429","post","type-post","status-publish","format-standard","hentry","category-server","tag-mongodb","tag-3368","tag-323","tag-190","tag-100","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MongoDB \u5e94\u7528\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/38429.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB \u5e94\u7528\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb283\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Java\u9879\u76ee\u4e2d\u901a\u8fc7Maven\u5f15\u5165MongoDB\u9a71\u52a8\uff0c\u5e76\u5c55\u793a\u4e86\u539f\u751fJava\u5ba2\u6237\u7aef\u548cSpring Boot\u6574\u5408MongoDB\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u5728\u539f\u751fJava\u5ba2\u6237\u7aef\u4e2d\uff0c\u901a\u8fc7MongoClients.create()\u65b9\u6cd5\u8fde\u63a5MongoDB\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u67e5\u8be2\u548c\u66f4\u65b0\u6587\u6863\u3002\u5728Spring Boot\u9879\u76ee\u4e2d\uff0c\u901a\u8fc7\u914d\u7f6e\u7c7bAppConfig\u5b9a\u4e49MongoClient\u548cMongoTemplate\uff0c\u5e76\u4f7f\u7528MongoTemplate\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u6d4b\u8bd5\u7c7bApplicationRunnerTest\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528M_mogodb client csdn\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/38429.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T05:02:28+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38429.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/38429.html\",\"name\":\"MongoDB \u5e94\u7528\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-20T05:02:28+00:00\",\"dateModified\":\"2025-05-20T05:02:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/38429.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/38429.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/38429.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MongoDB \u5e94\u7528\u5b9e\u6218\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB \u5e94\u7528\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/38429.html","og_locale":"zh_CN","og_type":"article","og_title":"MongoDB \u5e94\u7528\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb283\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Java\u9879\u76ee\u4e2d\u901a\u8fc7Maven\u5f15\u5165MongoDB\u9a71\u52a8\uff0c\u5e76\u5c55\u793a\u4e86\u539f\u751fJava\u5ba2\u6237\u7aef\u548cSpring Boot\u6574\u5408MongoDB\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u5728\u539f\u751fJava\u5ba2\u6237\u7aef\u4e2d\uff0c\u901a\u8fc7MongoClients.create()\u65b9\u6cd5\u8fde\u63a5MongoDB\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u67e5\u8be2\u548c\u66f4\u65b0\u6587\u6863\u3002\u5728Spring Boot\u9879\u76ee\u4e2d\uff0c\u901a\u8fc7\u914d\u7f6e\u7c7bAppConfig\u5b9a\u4e49MongoClient\u548cMongoTemplate\uff0c\u5e76\u4f7f\u7528MongoTemplate\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u6d4b\u8bd5\u7c7bApplicationRunnerTest\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528M_mogodb client csdn","og_url":"https:\/\/www.wsisp.com\/helps\/38429.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-20T05:02:28+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/38429.html","url":"https:\/\/www.wsisp.com\/helps\/38429.html","name":"MongoDB \u5e94\u7528\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-20T05:02:28+00:00","dateModified":"2025-05-20T05:02:28+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/38429.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/38429.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/38429.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"MongoDB \u5e94\u7528\u5b9e\u6218"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=38429"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/38429\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=38429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=38429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=38429"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=38429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}