我有一个用 Java 编写的 MongoDB 查询,我试图将不同的结果保存到变量中。例如,“Item”中的值应该保存到名称为 ItemName 的字符串中。 “Seller”中的值被保存到另一个变量中。我该怎么做呢?
这里我有一个查询...
// Database Name
MongoDatabase database = mongoClient.getDatabase("Marketplace");
// Collection name
MongoCollection collection = database.getCollection("Sales");
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
查询的结果是...
{"_id": {"$oid": "5e74ae1f1c410c6af0670d9b"}, "Item": "Soap", "Seller": "Antoine", "Size": "null", "Condition": "New", "BIN": "100", "BID": "50", "Link": "null"}
最佳答案
对于自动映射,您可以使用例如 Spring Data MongoDB .
Morphia是另一种选择。
关于java - 将MongoDB查询结果保存到Java中的不同变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60774706/