0

Problem

1)I have to display the count of total users with Incomplete and Complete Profiles.

2)Incomplete Profiles are those which will have 4 fields of String type. Other than that all are Complete Profiles.

Below is my model class fields

@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;

Code I have written:

1)In MySql I can apply a direct query which is similar to

a)Select count(*) from X where a is NULL or b IS NULL -- > ForIncompleteProfiles

b)Select count(*) from X where a IS NOT NULL or b IS NOT NULL -- > For CompleteProfiles

But here,

2)I can use findAll() method to retrieve all users in a List of User and then check for each user using if user.getA()or user.getB() any one of the field is null.IF yes,Count for Incomplete Profiles else Count for Completebb Profiles.

Then,I can return both these count's to a frontEndUSer in a HashMap

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.

Below is my code for the same

@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;
}
Jalaj Chawla
  • 189
  • 1
  • 3
  • 18

1 Answers1

1

You can solve your Problem with Spring Data MongoDb. Take a look here .

I'm assuming that you use a repository.

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

I think this should work. Try this out otherwise show into the Docs and find the best Combination.

Muhammed Misir
  • 400
  • 9
  • 22