我对 MongoDb 还很陌生。我使用 find 并获取 JSON 格式的结果。
{"Name": "Harshil", "Age":20}
所以我需要的是在java中解析它并获取变量中的值。
String name should contain Harshil
int age should contain 20
有没有办法将这些详细信息存储在 JAVA 对象中?
最佳答案
以下是连接到 MongoDB 的方法:
MongoClient client = new MongoClient("localhost",27017); //with default server and port adress
DB db = client.getDB( "your_db_name" );
DBCollection collection = db.getCollection("Your_Collection_Name");
连接后,您可以从服务器提取数据。下面,我假设您的文档有“姓名”和“年龄”字段:
DBObject dbo = collection.findOne();
String name = dbo.get("Name");
int age = dbo.get("Age");
关于java - MongoDB将数据获取到java对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19510832/