保存MongoDB集合后,如何将其加载到Java程序数据结构中?
我开始使用monogDB在java程序中保存和加载文件,但在将数据库加载回程序时遇到了问题。当我通过Mongo Compass进行检查时,我的save方法完全有效并创建了Mongo数据库,我的主要问题是我希望它将这些数据库值加载回我的数组中,该数组在运行代码时存储所有信息 现在,当我关闭并打开Java代码并输入load方法时,它不会检索保存的条目保存MongoDB集合后,如何将其加载到Java程序数据结构中?,java,arrays,database,mongodb,nosql,Java,Arrays,Database,Mongodb,Nosql,我开始使用monogDB在java程序中保存和加载文件,但在将数据库加载回程序时遇到了问题。当我通过Mongo Compass进行检查时,我的save方法完全有效并创建了Mongo数据库,我的主要问题是我希望它将这些数据库值加载回我的数组中,该数组在运行代码时存储所有信息 现在,当我关闭并打开Java代码并输入load方法时,它不会检索保存的条目 有人能帮我吗?我更新了您的加载文件方法以获取名称数组 private static void loadFile(String[] name) {
有人能帮我吗?我更新了您的
加载文件
方法以获取名称数组
private static void loadFile(String[] name) {
try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
MongoDatabase sampleTrainingDB = mongoClient.getDatabase("testDB");
MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("collection");
// find one document with new Document
Document document = gradesCollection.find(new Document("title", "Customer")).first();
/** Code added from here to the end of the method **/
/** Find all documents and get the names into an array **/
// First we will load the documents in to a List collection
List<Document> list = new ArrayList<>();
gradesCollection.find(new Document("title", "Customer"))
.into(list);
// From the list of we extract the name field and store in a string array
String [] names = list.stream()
.map(doc -> doc.get("name"))
.toArray(String[]::new);
System.out.println(Arrays.toString(names));
}
}
私有静态void加载文件(字符串[]名称){
try(MongoClient MongoClient=MongoClients.create(System.getProperty(“mongodb.uri”)){
MongoDatabase sampleTrainingDB=mongoClient.getDatabase(“testDB”);
MongoCollection gradesCollection=sampleTrainingDB.getCollection(“collection”);
//查找一个包含新文档的文档
Document Document=gradesCollection.find(新文档(“标题”、“客户”)).first();
/**从此处添加到方法末尾的代码**/
/**查找所有文档并将名称放入数组中**/
//首先,我们将把文档加载到列表集合中
列表=新的ArrayList();
gradesCollection.find(新文档(“标题”、“客户”))
.列入(名单);
//从列表中,我们提取名称字段并存储在字符串数组中
字符串[]名称=list.stream()
.map(doc->doc.get(“名称”))
.toArray(字符串[]::新建);
System.out.println(Arrays.toString(names));
}
}
请注意,我还对
MongoClient
创建方法进行了更改。是否希望所有文档中的name
字段值返回到一个数组中?加载方法为“是”
private static void saveFile(String[] name) {
// Creating a Mongo client
MongoClient mongo = new MongoClient("localhost", 27017);
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "trainDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
//Accessing the database
MongoDatabase database = mongo.getDatabase("testDB");
//Creating a collection
System.out.println("Collection created successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("collection");
System.out.println("Collection myCollection selected successfully");
for (int i = 0; i < 42; i++) {
if (!name[i].equals("vacant") && (i == 0 || !name[i - 1].equals(name[i]))) {
Document document = new Document("title", "Customer")
.append("Name", name[i])
.append("Seats", i + 1);
collection.insertOne(document);
}
}
System.out.println("Document inserted successfully");
}
private static void loadFile(String[] name) {
try (MongoClient mongoClient = MongoClients.create(System.getProperty("mongodb.uri"))) {
MongoDatabase sampleTrainingDB = mongoClient.getDatabase("testDB");
MongoCollection<Document> gradesCollection = sampleTrainingDB.getCollection("collection");
// find one document with new Document
Document document = gradesCollection.find(new Document("title", "Customer")).first();
/** Code added from here to the end of the method **/
/** Find all documents and get the names into an array **/
// First we will load the documents in to a List collection
List<Document> list = new ArrayList<>();
gradesCollection.find(new Document("title", "Customer"))
.into(list);
// From the list of we extract the name field and store in a string array
String [] names = list.stream()
.map(doc -> doc.get("name"))
.toArray(String[]::new);
System.out.println(Arrays.toString(names));
}
}