查询嵌套文档和 atrihute do 变量(MongoDB Java Driver 3.2)
Querying on nested documents and atrihute do variables (MongoDB Java Driver 3.2)
我有下面的数据,我正在尝试读取数据并将每个数据分配到变量中,但无法获取 info
中的数据,仍然给我 null
或空白 email
和 phone
{
"_id": ObjectId ( "56c570d904df6c51b463da54"),
"Name": "John Smith"
"Age": 31,
"Info": {
"Email", "john.smith@mail.com"
"Phone", "+987654321"
}
}
java代码下方
findAll.forEach (new Block <Document> () {
@Override
public void apply (Document document) {
String name = document.getString ( "name")!= null? document.getString ( "name"): "";
int age = document.getInteger ( "age")!= null? document.getInteger ( "age"): 0;
String email = document.getString ( "info.email")!= null? document.getString ( "info.email") "";
String phone = document.getString ( "info.phone")! = null? document.getString ( "info.phone") "";
Costumer costumer = new Costumer (name, age, email, phone);
System.out.println (costumer);
}
});
看起来很接近!代码中有几个错误:
- 将
Null
更改为null
- 您无法查找嵌套键,例如:
document.getString( "info.email")
将不起作用。您应该将子文档分配给一个变量,然后尝试从中获取数据。
我还没有测试过,但我认为这样的东西应该效果更好:
findAll.forEach (new Block <Document> () {
@Override
public void apply (Document document) {
String name = document.getString ("name")! = null? document.getString( "name") : "";
int age = document.getInteger ( "age", 0);
// Get the info subdocument
Document info = document.get("info", Document.class);
String email = "";
String phone = "";
if (info != null) {
email = info.getString ("email")! = null? info.getString("email") : "";
phone = info.getString ("phone")! = null? info.getString("phone") : "";
}
Costumer costumer = new Costumer (name, age, email, phone);
System.out.println(costumer);
}
});
希望对您有所帮助。
我有下面的数据,我正在尝试读取数据并将每个数据分配到变量中,但无法获取 info
中的数据,仍然给我 null
或空白 email
和 phone
{
"_id": ObjectId ( "56c570d904df6c51b463da54"),
"Name": "John Smith"
"Age": 31,
"Info": {
"Email", "john.smith@mail.com"
"Phone", "+987654321"
}
}
java代码下方
findAll.forEach (new Block <Document> () {
@Override
public void apply (Document document) {
String name = document.getString ( "name")!= null? document.getString ( "name"): "";
int age = document.getInteger ( "age")!= null? document.getInteger ( "age"): 0;
String email = document.getString ( "info.email")!= null? document.getString ( "info.email") "";
String phone = document.getString ( "info.phone")! = null? document.getString ( "info.phone") "";
Costumer costumer = new Costumer (name, age, email, phone);
System.out.println (costumer);
}
});
看起来很接近!代码中有几个错误:
- 将
Null
更改为null
- 您无法查找嵌套键,例如:
document.getString( "info.email")
将不起作用。您应该将子文档分配给一个变量,然后尝试从中获取数据。
我还没有测试过,但我认为这样的东西应该效果更好:
findAll.forEach (new Block <Document> () {
@Override
public void apply (Document document) {
String name = document.getString ("name")! = null? document.getString( "name") : "";
int age = document.getInteger ( "age", 0);
// Get the info subdocument
Document info = document.get("info", Document.class);
String email = "";
String phone = "";
if (info != null) {
email = info.getString ("email")! = null? info.getString("email") : "";
phone = info.getString ("phone")! = null? info.getString("phone") : "";
}
Costumer costumer = new Costumer (name, age, email, phone);
System.out.println(costumer);
}
});
希望对您有所帮助。