0

Working with sprint data mongo.

MongoDb version - 4.0
Sprint data version 2.0.4.RELEASE

Is it possible to return new array of element that match "search condition" dynamically.

example: I've object that fields are generated dynamically , E.g: f1!=null ,f2!=null..,f3=null

public class SearchDto {
    private int width;
    private int height;
    private String diameter;
    private String brand;..

I want to match all element according to not null values.

// The Json:

   [ {

            "id": "5b67wqe2316d05fac",
            "address": "qwewq",
            "phoneNumber": "0wqe3734",
            "name": "qweqw",

            "tireList": [
                  {
                    "loadIndex": 88,
                    "speedIndex": "V",
                    "width": 45,
                    "height": 15,
                    "diameter": "R22",
                    "price": 1500
                  },
                  {
                    "barcode": "Pirreligt45225R2065V",
                    "imageURL": "https://mxxx936568661",
                    "loadIndex": 65,
                    "speedIndex": "V",
                    "width": 45,
                    "height": 300,
                    "diameter": "R20",
                    "price": 1500
                  },
                  {
                    "imageURL": "https://maxxxx36367787",
                    "manufacturer": "good year",
                    "loadIndex": 80,
                   "width": 45,
                   "height": 225,
                    "diameter": "R20",
                    "speedIndex": "V",
                    "price": 1200
                  },...

    .......
    ......
                ]

the code:

 Query query = new Query();

        Criteria where = Criteria.where("id").is(garageId);

        List<Criteria> all = new ArrayList<>();
        if (searchDto.getDiameter() != null) {
            all.add(Criteria.where("tireList").elemMatch(Criteria.where("diameter").is(searchDto.getDiameter())));
        }
        if (searchDto.getHeight() > 0) {
            all.add(Criteria.where("tireList").elemMatch(Criteria.where("height").is(searchDto.getHeight())));
        }
        if (searchDto.getWidth() > 0) {
            all.add(Criteria.where("tireList").elemMatch(Criteria.where("width").is(searchDto.getWidth())));
        }
        if (searchDto.isBrand() != null) {
            boolean brand = Boolean.getBoolean(searchDto.isBrand());
            all.add(Criteria.where("tireList").elemMatch(Criteria.where("brand").is(brand)));
        }
        query.addCriteria(where.andOperator(all.toArray(new Criteria[all.size()])));

// here I got empty []

   List<TireDto> tireDtos = mongoTemplate.find(query, TireDto.class);

only if I change class got the result , but I need matched new array[]

    GarageDto tireDtos = mongoTemplate.findOne(query, GarageDto.class);

and the classes are:

public class GarageDto {
    @Id
    @NotNull
    @Indexed
    private String id;
    @Indexed
    private List<TireDto> tireList = new ArrayList<>();

please advice - preferably Spring data mongo

VitalyT
  • 1,671
  • 3
  • 21
  • 49
  • elemMatch only returns one matching element not array. Your query also looks incorrect ( did you mean or instead of and ) and you are missing projection as well. Can you explain what you are trying to do ? – s7vr Aug 22 '18 at 13:13
  • @ Veeram , Trying to do logical "and" what stands with criteria , in other words try to get all elements inside given array according to "SearchDto"/maches to fields – VitalyT Aug 22 '18 at 13:25

0 Answers0