簡體   English   中英

如何克服這個錯誤“com.mongodb.diagnostics.logging.JULLogger 日志”

[英]how overcome this error "com.mongodb.diagnostics.logging.JULLogger log"

我正在使用與 MongoDB 連接的 Java 程序,當我運行該程序時,它會顯示錯誤但代碼正在運行。 MongoDB 有一個名為 MongoDB 的數據庫,其中有一個名為 Seatbooking 的集合,有兩列(名稱,座位號)。 這是我的代碼:

MongoClient mongoClient = new MongoClient("localhost", 27017);
System.out.println("connection is established");

MongoDatabase mongoDatabase = mongoClient.getDatabase("MongoDB");
MongoCollection mongoCollection = mongoDatabase.getCollection("seatbooking");

Document document = new Document("name","shenal");
document.append("seatnumber",20);
mongoCollection.insertOne(document);

當我運行此代碼時,我的輸出是:

> Mar 09, 2020 12:41:36 PM
> com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster created
> with settings {hosts=[localhost:27017], mode=SINGLE,
> requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms',
> maxWaitQueueSize=500}
> **connection is established** Mar 09, 2020 12:41:36 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Cluster
> description not yet available. Waiting for 30000 ms before timing out
> Mar 09, 2020 12:41:36 PM com.mongodb.diagnostics.logging.JULLogger log
> INFO: Opened connection [connectionId{localValue:1, serverValue:309}]
> to localhost:27017 Mar 09, 2020 12:41:36 PM
> com.mongodb.diagnostics.logging.JULLogger log INFO: Monitor thread
> successfully connected to server with description
> ServerDescription{address=localhost:27017, type=STANDALONE,
> state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2,
> 2]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216,
> logicalSessionTimeoutMinutes=30, roundTripTimeNanos=5168100} Mar 09,
> 2020 12:41:36 PM com.mongodb.diagnostics.logging.JULLogger log INFO:
> Opened connection [connectionId{localValue:2, serverValue:310}] to
> localhost:27017

日志輸出不顯示insertOne操作的任何結果。

如果您使用的是 4.0 之前的 MongoDB Java 驅動程序版本,則不會返回確認對象。 但是,版本 4 的insertOne方法返回一個InsertOneResult對象。 這是 4.0 中的新功能(在先前版本中,該方法返回了一個void )。

您可以使用以下代碼來檢查插入的結果如下(使用版本 4.0):

try {
    InsertOneResult insertResult = collection.insertOne(document);
    System.out.println("Document inserted with ID: " + insertResult.getInsertedId());
}
catch(MongoWriteException e) {
    // write failure happened, handle it here...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM