Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从mongoDB检索嵌入的文档_Java_Mongodb_Spring Mvc_Spring Data - Fatal编程技术网

Java 从mongoDB检索嵌入的文档

Java 从mongoDB检索嵌入的文档,java,mongodb,spring-mvc,spring-data,Java,Mongodb,Spring Mvc,Spring Data,以下数据存储在MongoDB中。我正在使用Spring数据并将数据存储到mongoDB中。 如果我想检索字段(“id”或“Name”),我可以这样做,但是如果我想检索子字段(“firstName”或“lastName”),我不能 (例如)如果我想从下面的数据中检索子字段“lastName”,我不能。请在这方面帮助我。 提前谢谢 MongoDB中存储的数据: { "id":101, "name": {"firstName":"Mark", "lastName":"Anton

以下数据存储在MongoDB中。我正在使用Spring数据并将数据存储到mongoDB中。 如果我想检索字段(“id”或“Name”),我可以这样做,但是如果我想检索子字段(“firstName”或“lastName”),我不能

(例如)如果我想从下面的数据中检索子字段“lastName”,我不能。请在这方面帮助我。 提前谢谢

MongoDB中存储的数据:

{

"id":101,

"name": {"firstName":"Mark",

         "lastName":"Antony"

         }
}
我使用的代码是:

PersonService.java

public List<Audit> searchPerson(Audit audit)
{
List<NameDetails> name=audit.getName();
return mongoTemplate.find(new Query(Criteria.where("name.lastName").is(name.get(0))), Audit.class,COLLECTION_NAME); 
}
Audit.java

@Document
public class Audit {
@Id
private String id;
private List<NameDetails> name;

public String getId() {
    System.out.println("Person: getId");
    return id;
}
public void setId(String id) {
    System.out.println("Person: setId");
    this.id = id;
}
public List<NameDetails> getName() {
    System.out.println("Audit: getName");
    return name;
}
public void setName(List<NameDetails> name) {
    System.out.println("Audit: setName");
    this.name = name;
}       

}
(output.jsp)UI页面


名称
${person.id}
${person.lastName}
更改

return mongoTemplate.find(
    new Query(Criteria.where("name.lastName").is(name.get(0))), 
    Audit.class,COLLECTION_NAME);


您正在尝试使用整个
名称详细信息
,而不仅仅是姓氏。

感谢您的关注。我已经按照你说的做了,但是它抛出了空指针,除了它在返回语句中抛出NPE?的哪一行。此行出错:返回mongoTemplate.find(新查询(条件.where(“name.lastName”).is(name.get(0.getLastName())),Audit.class,集合\u name);提供堆栈跟踪。如果mongoTemplate为null、list为空或lastName为null,则这可能导致NPE(如果
则将NPE设置为null。我正在从UI的文本框中传递lastName,并尝试根据lastName检索数据。未设置该lastName。如果我想传递id并仅检索该id的lastName,我该怎么办。我是新手,请帮助我。
package com.register.mongo.model;

public class NameDetails {
private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
System.out.println("NameDetails: setFirstName");
this.firstName = firstName;
}
public String getLastName() {
System.out.println("NameDetails: getLastName");
return lastName;
}
public void setLastName(String lastName) {
System.out.println("NameDetails: setLastName");
this.lastName = lastName;
}   
}
<form action="person/search" method="get">
<table>
<tr><td>
<label>Name</label>
<input type="text" id="lastName" name="lastName"/>
</td></tr>
<tr><td>
<input type="submit" value="Search"/>
</td></tr>
</table>
</form>    

<table border="2">
<c:forEach var="person" items="${personList}">
<tr>
<td>${person.id}</td> 
</tr>
<tr>
<td>${person.lastName}</td> 
</tr>
</c:forEach>
</table>    
return mongoTemplate.find(
    new Query(Criteria.where("name.lastName").is(name.get(0))), 
    Audit.class,COLLECTION_NAME);
return mongoTemplate.find(
    new Query(Criteria.where("name.lastName").is(name.get(0).getLastName())), 
    Audit.class,COLLECTION_NAME);