我对 MongoDB 相当陌生,我遇到了如何将数组中的数据存储到 mongodb 数据库中的问题?由于某种原因,当我实现以下代码时,出现很多错误。
private void storeData(String[] trainSeats, String[] customerName, String[] traincustomerSeats){
MongoClient dbb = new MongoClient("localhost", 27017);
DB dbs = dbb.getDB("Train Customers and Seats");
DBCollection coll1 = dbs.getCollection("CW1");
DBCollection coll2 = dbs.getCollection("CW2");
DBCollection coll3 = dbs.getCollection("CW3");
BasicDBObject oneDoc = new BasicDBObject();
for (int k = 1; k < trainSeats.length; k++){
oneDoc.append(String.valueOf(k), trainSeats[k]);
}
coll1.insert(oneDoc);
BasicDBObject twoDoc = new BasicDBObject();
for(int k = 1; k < customerName.length; k++){
twoDoc.append(String.valueOf(k), customerName[k]);
}
coll2.insert(twoDoc);
BasicDBObject threeDoc = new BasicDBObject();
for(int k = 1; k < traincustomerSeats.length; k++){
threeDoc.append(String.valueOf(k), traincustomerSeats[k]);
}
coll3.insert(threeDoc);
optionEnter(trainSeats,customerName,traincustomerSeats);
}
以下是我运行上述代码时遇到的错误:
最佳答案
我看到了你的错误日志。您收到此错误的原因是您的数据库名称“Train Customers and Seats” 有大量空格 ' ',这是数据库中不允许的名称。
将数据库重命名为“Train_Customers_and_Seats”或“TrainCustomersAndSeats”
关于java - 如何使用 Java 将数据存储到 MongoDb 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60732771/