使用 spring 引导和 mongodb 检索具有完整和不完整配置文件的用户数的最佳方法是什么?

What is the best way to retrieve the count of users with complete and incomplete profiles using spring boot and mongodb?

问题

1)我必须显示个人资料不完整和完整的用户总数。

2) 不完整的配置文件是那些将具有 4 个字符串类型字段的配置文件。 除此之外都是完整的配置文件。

下面是我的模型class字段

@Id
private String id;
private String[] imageIds;
private String concerns;
private String summary;
private String likings;
private String occupation;
private String religion;
private String education;
private String height;
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private double[] location;
private INTERESTS[] interests;
private String fcmId;
private List connections;
private List declined;
private List pending;

我写的代码:

1)在MySql中我可以应用类似于

的直接查询

a)Select 来自 X 的计数 (*),其中 a 为 NULL 或 b 为 NULL --> ForIncompleteProfiles

b)Select 来自 X 的计数 (*),其中 a 不为空或 b 不为空 --> 对于 CompleteProfiles

但是在这里,

2) 我可以使用 findAll() 方法检索用户列表中的所有用户,然后使用 if user.getA() 或 user.getB() 中的任何一个检查每个用户字段是 null.IF 是的,计算不完整的配置文件,否则计算 Completebb 配置文件。

然后,我可以return将这两个计数都计算到 HashMap 中的 frontEndUSer

Can anyone guide me if this is a correct way and if not how to do it in a more efficient way and effective way using spring boot and MongoData Repository.

下面是我的代码

@RequestMapping(value = "/showprofiles", method = RequestMethod.GET)
public Map showProfiles(HttpServletResponse response) {
    int pageSize=2000;
    Page<User> users=null;
    int page=0;
    int countInComplete=0;
    int countComplete=0;
    Map hm=null;
    users = userService.getAllUsers(new PageRequest(page,pageSize));
    do {
        users = userService.getAllUsers(new PageRequest(page,pageSize));
        for (User user : users) {
            if (user.getName() == null || user.getGender() == null || user.getInterests() == null || user.getImageIds() == null) {
                countInComplete++;
            }
            else{
                countComplete++;
            }
        }
        page++;
    }while (users.hasNext());
    hm=new HashMap();
    hm.put("countComplete",countComplete);
    hm.put("countInComplete",countInComplete);
    return hm;
}

您可以使用 Spring 数据 MongoDb 解决您的问题。看看here .

我假设您使用存储库。

@Annotation public interface UserRepository extends ... {   
  List<User> findByNameNullOrGenderNullOrInterestsNullOrImageIdsNull(); 
}

我认为这应该可行。试试这个,否则显示在文档中并找到最佳组合。