Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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
Mongodb 在SpringBoot项目中,MongoTemplate返回userId null_Mongodb_Spring Boot_Mongotemplate - Fatal编程技术网

Mongodb 在SpringBoot项目中,MongoTemplate返回userId null

Mongodb 在SpringBoot项目中,MongoTemplate返回userId null,mongodb,spring-boot,mongotemplate,Mongodb,Spring Boot,Mongotemplate,我使用spring boot和mongoDB,我尝试使用MongoTemplate发出一个复杂的请求,以获得所有没有空项目的投资组合,但对于所有结果,我收到的userId为null,我不知道我错在哪里。我是springboot的新手,任何帮助都将不胜感激 当我执行一个简单的请求时,比如List portfolios=portfolioRepository.findAll();用户ID不是空的 这是我的代码: PortfolioRepositoryImpl(带MongoTemplate) publ

我使用spring boot和mongoDB,我尝试使用MongoTemplate发出一个复杂的请求,以获得所有没有空项目的投资组合,但对于所有结果,我收到的userId为null,我不知道我错在哪里。我是springboot的新手,任何帮助都将不胜感激

当我执行一个简单的请求时,比如List portfolios=portfolioRepository.findAll();用户ID不是空的

这是我的代码:

PortfolioRepositoryImpl(带MongoTemplate)

public List getPortfolioWithNoEmptyProjects(){
查询=新查询();
query.addCriteria(Criteria.where(“projects”).exists(true).ne(new ArrayList());
query.fields().elemMatch(“projects”,Criteria.where(“online”).is(true));
//query.with(新排序(Sort.Direction.ASC,“已创建”);
返回mongoTemplate.find(查询,Portfolio.class);
}
投资组合文档

@Document
public class Portfolio {

    @Id
    @Getter
    private String id;

    @Getter
    @Setter
    @NotBlank(message = "media.user.id.not.be.null.or.empty")
    private String userId;

    @Getter
    @Setter
    @NotNull(message = "portfolio.projects.not.be.null")
    private ArrayList<Project> projects;

    public Portfolio() {
        this.projects = new ArrayList<>();
    }

}
@文档
公课档案{
@身份证
@吸气剂
私有字符串id;
@吸气剂
@塞特
@NotBlank(message=“media.user.id.not.be.null.or.empty”)
私有字符串用户标识;
@吸气剂
@塞特
@NotNull(message=“portfolio.projects.not.be.null”)
私人ArrayList项目;
公共投资组合(){
this.projects=new ArrayList();
}
}
文档的结构

@Document
public class Portfolio {

    @Id
    @Getter
    private String id;

    @Getter
    @Setter
    @NotBlank(message = "media.user.id.not.be.null.or.empty")
    private String userId;

    @Getter
    @Setter
    @NotNull(message = "portfolio.projects.not.be.null")
    private ArrayList<Project> projects;

    public Portfolio() {
        this.projects = new ArrayList<>();
    }

}

结果

@Document
public class Portfolio {

    @Id
    @Getter
    private String id;

    @Getter
    @Setter
    @NotBlank(message = "media.user.id.not.be.null.or.empty")
    private String userId;

    @Getter
    @Setter
    @NotNull(message = "portfolio.projects.not.be.null")
    private ArrayList<Project> projects;

    public Portfolio() {
        this.projects = new ArrayList<>();
    }

}

我发现我的问题只是需要包括以下预期字段

Query query = new Query();
query.fields().include("user");
query.addCriteria(Criteria.where("projects").exists(true).ne(new ArrayList<>()));
query.fields().elemMatch("projects", Criteria.where("online").is(true));
return mongoTemplate.find(query, Portfolio.class);
Query Query=new Query();
query.fields().包括(“用户”);
query.addCriteria(Criteria.where(“projects”).exists(true).ne(new ArrayList());
query.fields().elemMatch(“projects”,Criteria.where(“online”).is(true));
返回mongoTemplate.find(查询,Portfolio.class);