舒凡人生 發表於 2020-10-29 12:41:00

MongoDB Java连接---MongoDB基础用法(四)

<h2 id="mongodb-连接">MongoDB 连接</h2>
<p>标准 URI 连接语法:</p>
<pre><code>mongodb://host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[?options]]
</code></pre>
<ul>
<li><strong>mongodb://</strong> 这是固定的格式,必须要指定。</li>
<li><strong>username:password@</strong> 可选项,如果设置,在连接数据库服务器之后,驱动都会尝试登录这个数据库</li>
<li><strong>host1</strong> 必须的指定至少一个host, host1 是这个URI唯一要填写的。它指定了要连接服务器的地址。如果要连接复制集,请指定多个主机地址。</li>
<li><strong>portX</strong> 可选的指定端口,如果不填,默认为27017</li>
<li><strong>/database</strong> 如果指定username:password@,连接并验证登录指定数据库。若不指定,默认打开 test 数据库。</li>
<li><strong>?options</strong> 是连接选项。如果不使用/database,则前面需要加上/。所有连接选项都是键值对name=value,键值对之间通过&amp;或;(分号)隔开</li>
</ul>
<p>标准的连接格式包含了多个选项(options),如下所示:</p>
<table>
<thead>
<tr>
<th>选项</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>replicaSet=name</td>
<td>验证replica set的名称。 Impliesconnect=replicaSet.</td>
</tr>
<tr>
<td>slaveOk=true</td>
<td>false</td>
</tr>
<tr>
<td>safe=true</td>
<td>false</td>
</tr>
<tr>
<td>w=n</td>
<td>驱动添加 { w : n } 到getLastError命令. 应用于safe=true。</td>
</tr>
<tr>
<td>wtimeoutMS=ms</td>
<td>驱动添加 { wtimeout : ms } 到 getlasterror 命令. 应用于 safe=true.</td>
</tr>
<tr>
<td>fsync=true</td>
<td>false</td>
</tr>
<tr>
<td>journal=true</td>
<td>false</td>
</tr>
<tr>
<td>connectTimeoutMS=ms</td>
<td>可以打开连接的时间。</td>
</tr>
<tr>
<td>socketTimeoutMS=ms</td>
<td>发送和接受sockets的时间。</td>
</tr>
</tbody>
</table>
<h2 id="mongodb-java">MongoDB Java</h2>
<h3 id="环境配置">环境配置</h3>
<p>在 Java 程序中如果要使用 MongoDB,你需要确保已经安装了 Java 环境及 MongoDB JDBC 驱动。</p>
<p>本章节实例适合 Mongo 3.x 以上版本。</p>
<p>现在让我们来检测你是否安装了 MongoDB JDBC 驱动。</p>
<ul>
<li>首先你必须下载mongo jar包,下载地址:https://mongodb.github.io/mongo-java-driver/, 请确保下载最新版本。<br>
<img src="https://b3logfile.com/file/2020/10/downlaodmongojava-6a3d5183.jpg?imageView2/2/w/1280/format/jpg/interlace/1/q/100" alt="downlaodmongojava.jpg" loading="lazy"></li>
<li>你需要将 mongo-java-driver-3.2.2.jar (找到合适的版本)包含在你的 classpath 中。。</li>
<li>国内 mongodb-driver jar 下载地址:http://central.maven.org/maven2/org/mongodb/mongo-java-driver/</li>
</ul>
<h3 id="连接数据库">连接数据库</h3>
<p>连接数据库,你需要指定数据库名称,如果指定的数据库不存在,mongo会自动创建数据库。</p>
<p>连接数据库的Java代码如下:</p>
<pre><code>import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp; &nbsp; // 连接到 mongodb 服务
&nbsp; &nbsp; &nbsp; &nbsp; MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到数据库
&nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp; &nbsp; System.out.println("Connect to database successfully");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp;System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp; }
&nbsp; }
}
</code></pre>
<p>现在,让我们来编译运行程序并连接到数据库 mycol。</p>
<p>你可以根据你的实际环境改变 MongoDB JDBC 驱动的路径。</p>
<p>本实例将 MongoDB JDBC 启动包 mongo-java-driver-3.2.2.jar 放在本地目录下:</p>
<pre><code>$ javac -cp .:mongo-java-driver-3.2.2.jar MongoDBJDBC.java
$ java -cp .:mongo-java-driver-3.2.2.jar MongoDBJDBC
Connect to database successfully
Authentication: true
</code></pre>
<p>本实例中 Mongo 数据库无需用户名密码验证。如果你的 Mongo 需要验证用户名及密码,可以使用以下代码:</p>
<pre><code>import java.util.ArrayList; &nbsp;
import java.util.List; &nbsp;
import com.mongodb.MongoClient; &nbsp;
import com.mongodb.MongoCredential; &nbsp;
import com.mongodb.ServerAddress; &nbsp;
import com.mongodb.client.MongoDatabase; &nbsp;
&nbsp;
public class MongoDBJDBC { &nbsp;
&nbsp;public static void main(String[] args){ &nbsp;
&nbsp; &nbsp; &nbsp;try { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//ServerAddress()两个参数分别为 服务器地址 和 端口 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ServerAddress serverAddress = new ServerAddress("localhost",27017); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List&lt;ServerAddress&gt; addrs = new ArrayList&lt;ServerAddress&gt;(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;addrs.add(serverAddress); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MongoCredential credential = MongoCredential.createScramSha1Credential("username", "databaseName", "password".toCharArray()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List&lt;MongoCredential&gt; credentials = new ArrayList&lt;MongoCredential&gt;(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;credentials.add(credential); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//通过连接认证获取MongoDB连接 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MongoClient mongoClient = new MongoClient(addrs,credentials); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//连接到数据库 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MongoDatabase mongoDatabase = mongoClient.getDatabase("databaseName"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Connect to database successfully"); &nbsp;
&nbsp; &nbsp; &nbsp;} catch (Exception e) { &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.err.println( e.getClass().getName() + ": " + e.getMessage() ); &nbsp;
&nbsp; &nbsp; &nbsp;} &nbsp;
&nbsp;} &nbsp;
}
</code></pre>
<h3 id="创建集合">创建集合</h3>
<p>我们可以使用 com.mongodb.client.MongoDatabase 类中的createCollection()来创建集合</p>
<p>代码片段如下:</p>
<pre><code>import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp;// 连接到 mongodb 服务
&nbsp; &nbsp;MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;// 连接到数据库
&nbsp; &nbsp;MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp;System.out.println("Connect to database successfully");
&nbsp; &nbsp;mongoDatabase.createCollection("test");
&nbsp; &nbsp;System.out.println("集合创建成功");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp;System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp; }
&nbsp; }
}
</code></pre>
<p>编译运行以上程序,输出结果如下:</p>
<pre><code>Connect to database successfully
集合创建成功
</code></pre>
<h3 id="获取集合">获取集合</h3>
<p>我们可以使用com.mongodb.client.MongoDatabase类的 getCollection() 方法来获取一个集合</p>
<p>代码片段如下:</p>
<pre><code>import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp; &nbsp; // 连接到 mongodb 服务
&nbsp; &nbsp; &nbsp; &nbsp; MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到数据库
&nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp; &nbsp; System.out.println("Connect to database successfully");
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection("test");
&nbsp; &nbsp; &nbsp; System.out.println("集合 test 选择成功");
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp;System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp; }
&nbsp; }
}
</code></pre>
<p>编译运行以上程序,输出结果如下:</p>
<pre><code>Connect to database successfully
集合 test 选择成功
</code></pre>
<h3 id="插入文档">插入文档</h3>
<p>我们可以使用com.mongodb.client.MongoCollection类的 insertMany() 方法来插入一个文档</p>
<p>代码片段如下:</p>
<pre><code>import java.util.ArrayList;
import java.util.List;
import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到 mongodb 服务
&nbsp; &nbsp; &nbsp; &nbsp; MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到数据库
&nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Connect to database successfully");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection("test");
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("集合 test 选择成功");
&nbsp; &nbsp; &nbsp; &nbsp; //插入文档 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp; &nbsp; * 1. 创建文档 org.bson.Document 参数为key-value的格式
&nbsp; &nbsp; &nbsp; &nbsp; * 2. 创建文档集合List&lt;Document&gt;
&nbsp; &nbsp; &nbsp; &nbsp; * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List&lt;Document&gt;) 插入单个文档可以用 mongoCollection.insertOne(Document)
&nbsp; &nbsp; &nbsp; &nbsp; * */
&nbsp; &nbsp; &nbsp; &nbsp; Document document = new Document("title", "MongoDB"). &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; append("description", "database"). &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; append("likes", 100). &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; append("by", "Fly"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; List&lt;Document&gt; documents = new ArrayList&lt;Document&gt;(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; documents.add(document); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; collection.insertMany(documents); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("文档插入成功"); &nbsp;
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp; &nbsp; System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp;}
&nbsp; }
}
</code></pre>
<p>编译运行以上程序,输出结果如下:</p>
<pre><code>Connect to database successfully
集合 test 选择成功
文档插入成功
</code></pre>
<h3 id="检索所有文档">检索所有文档</h3>
<p>我们可以使用 com.mongodb.client.MongoCollection 类中的 find() 方法来获取集合中的所有文档。</p>
<p>此方法返回一个游标,所以你需要遍历这个游标。</p>
<p>代码片段如下:</p>
<pre><code>import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到 mongodb 服务
&nbsp; &nbsp; &nbsp; &nbsp; MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到数据库
&nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Connect to database successfully");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection("test");
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("集合 test 选择成功");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; //检索所有文档 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp; &nbsp; * 1. 获取迭代器FindIterable&lt;Document&gt;
&nbsp; &nbsp; &nbsp; &nbsp; * 2. 获取游标MongoCursor&lt;Document&gt;
&nbsp; &nbsp; &nbsp; &nbsp; * 3. 通过游标遍历检索出的文档集合
&nbsp; &nbsp; &nbsp; &nbsp; * */ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; FindIterable&lt;Document&gt; findIterable = collection.find(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; MongoCursor&lt;Document&gt; mongoCursor = findIterable.iterator(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; while(mongoCursor.hasNext()){ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(mongoCursor.next()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp; &nbsp; System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp;}
&nbsp; }
}
</code></pre>
<p>编译运行以上程序,输出结果如下:</p>
<pre><code>Connect to database successfully
集合 test 选择成功
Document{{_id=56e65fb1fd57a86304fe2692, title=MongoDB, description=database, likes=100, by=Fly}}
</code></pre>
<h3 id="更新文档">更新文档</h3>
<p>你可以使用 com.mongodb.client.MongoCollection 类中的 updateMany() 方法来更新集合中的文档。</p>
<p>代码片段如下:</p>
<pre><code>import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到 mongodb 服务
&nbsp; &nbsp; &nbsp; &nbsp; MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到数据库
&nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Connect to database successfully");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection("test");
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("集合 test 选择成功");
&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; //更新文档 &nbsp; 将文档中likes=100的文档修改为likes=200 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200))); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; //检索查看结果 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; FindIterable&lt;Document&gt; findIterable = collection.find(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; MongoCursor&lt;Document&gt; mongoCursor = findIterable.iterator(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; while(mongoCursor.hasNext()){ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(mongoCursor.next()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp; &nbsp; System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp;}
&nbsp; }
}
</code></pre>
<p>编译运行以上程序,输出结果如下:</p>
<pre><code>Connect to database successfully
集合 test 选择成功
Document{{_id=56e65fb1fd57a86304fe2692, title=MongoDB, description=database, likes=200, by=Fly}}
</code></pre>
<h3 id="删除第一个文档">删除第一个文档</h3>
<p>要删除集合中的第一个文档,首先你需要使用com.mongodb.DBCollection类中的 findOne()方法来获取第一个文档,然后使用remove 方法删除。</p>
<p>代码片段如下:</p>
<pre><code>import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class MongoDBJDBC{
&nbsp; public static void main( String args[] ){
&nbsp; &nbsp;try{ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; // 连接到 mongodb 服务
&nbsp; &nbsp; &nbsp; &nbsp; MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

&nbsp; &nbsp; &nbsp; &nbsp; // 连接到数据库
&nbsp; &nbsp; &nbsp; &nbsp; MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Connect to database successfully");

&nbsp; &nbsp; &nbsp; &nbsp; MongoCollection&lt;Document&gt; collection = mongoDatabase.getCollection("test");
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("集合 test 选择成功");

&nbsp; &nbsp; &nbsp; &nbsp; //删除符合条件的第一个文档 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; collection.deleteOne(Filters.eq("likes", 200)); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; //删除所有符合条件的文档 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; collection.deleteMany (Filters.eq("likes", 200)); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; //检索查看结果 &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; FindIterable&lt;Document&gt; findIterable = collection.find(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; MongoCursor&lt;Document&gt; mongoCursor = findIterable.iterator(); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; while(mongoCursor.hasNext()){ &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mongoCursor.next()); &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;}catch(Exception e){
&nbsp; &nbsp; &nbsp;System.err.println( e.getClass().getName() + ": " + e.getMessage() );
&nbsp; &nbsp; }
&nbsp; }
}
</code></pre>
<p>编译运行以上程序,输出结果如下:</p>
<pre><code>Connect to database successfully
集合 test 选择成功
</code></pre>
<p>更多扩展:https://www.runoob.com/mongodb/mongodb-install-php-driver.html</p>


</div>
<div id="MySignature" role="contentinfo">
    你一定会喜欢那个因为喜欢她而发光的自己!<br />
个人博客:http://www.yanghelong.top<br><br>
来源:https://www.cnblogs.com/zzu-general/p/13896113.html
頁: [1]
查看完整版本: MongoDB Java连接---MongoDB基础用法(四)