I create a MongoDB collection with a validator.
db.createCollection("Student", {
validator: { $jsonSchema: {
bsonType: "Object",
required: ["name", "score"],
properties: {
name: {
bsonType: "string",
},
score: {
bsonType: "object",
required: ["History", "Math", "Computer"],
properties: {
History: {
bsonType: "int"
},
Math: {
bsonType: "int"
},
Computer: {
bsonType: "int"
}
}
}
}
}}
})
I want to insert this json.
{
"name": "scofield",
"score":{
"History": 45,
"Math": 89,
"Computer": 100
}
}
by this code
mport .bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class Add {
public static void main(String[] args) {
// link MongoDB
MongoClient mongoClient=new MongoClient("localhost",27017);
System.out.println("client");
// get MongoDatabase object
MongoDatabase database = mongoClient.getDatabase("experiment");
System.out.println("database");
// get MongoCollection object
MongoCollection<Document> collection = database.getCollection("Student");
System.out.println("collection");
Document studentDoc = new Document("name", "scofield")
.append("score", new Document("History", 45)
.append("Math", 89)
.append("Computer", 100));
System.out.println("create doc");
collection.insertOne(studentDoc);
System.out.println("insert");
mongoClient.close();
System.out.println("close");
}
}
but get this error:
Nov 20, 2024 4:42:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
client
database
collection
Nov 20, 2024 4:42:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Nov 20, 2024 4:42:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:10}] to localhost:27017
Nov 20, 2024 4:42:48 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, 4, 22]}, minWireVersion=0, maxWireVersion=9, maxDocumentSize=16777216, roundTripTimeNanos=592516}
Nov 20, 2024 4:42:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:11}] to localhost:27017
create doc
insert
Nov 20, 2024 4:42:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:2, serverValue:11}] to localhost:27017 because the pool has been closed.
close
Even when I change the collection without validator, I get the same error.
Then I simplify the data, and just input a key-value to collection at previous line, and get the same error
MongoDB version: 4.4.22
mongo-java-driver version: 3.2.2
I read the official documents and searched on google but found no solution yet.