Spring MongoDB returns empty Array

Refresh

February 2019

Views

5 time

0

I am trying to find All the Documents from Collection containing CropData, where cropName is RICE. this is mock Data generated by mockaroo, for testing API.

this is how my 1 out of 1000 Documents looks like :

{
    "_id" : {
        "ObjectId" : "5c603f99fc13ae49e8000000"
    },
    "_class" : "com.cheruvu.webapp.entity.Farmer",
    "farmerReferenceId" : "a69a63e4-bbcc-4162-9484-cfc5bb6f4647",
    "oneTimeDataEntered" : false,
    "firstName" : "Rennie",
    "phoneNumber" : "9822702624",
    "CropData" : {
        "crop" : "RICE",
        "cropName" : "RICE",
        "cropPrice" : "$74.75",
        "cropAcres" : 27.0
    },
    "loc" : {
        "type" : "Point",
        "coordinates" : [ 
            117.0, 
            -53.0
        ]
    }
}

this is my mongoDB working query :

db.getCollection('Farmer').find({"CropData.crop":"RICE"})

But, when i do this same thing in Spring via Query Object it returns in Empty Array, this is my Spring function :

public void returnFarmerList(String cropName, BasicResponse response) {
        Query query = new Query();
        query.addCriteria(Criteria.where(Farmer.Constants.CROP_DATA).exists(true));
        query.addCriteria(Criteria.where(CropData.Constants.CROP_NAME).is(cropName));
        response.setResponse(farmerDAO.runQuery(query, Farmer.class));
    }

result :

{
  "response":"[]"
}

0 answers