How to use $ redact with aggregation in Java with mongodb pattern

I can't add redact with the mongodb template below my mongo code, which works fine and uses sping mongo 1.8.5

  db.abc.aggregate([
 { "$geoNear" : { near : { "type" : "Point", "coordinates" : [ 72.5, 19.8 ] 
 }, distanceField: "dist.calculated", maxDistance: 500000, includeLocs: "dist.location", num: 5, limit: 200, spherical: true } },
{ "$unwind" : "$items" },
{ "$redact" : { 
"$cond" : { 
if : { "$eq" : [ { "$cmp" : [ "$items.address", "$dist.location" ] }, 0 ] },
then : "$$KEEP", 
else : "$$PRUNE"
 } 
} 
}
])

Here is my java code that does not work as above mongo code

 Point point=new Point(longi,lat);
    NearQuery nearQuery = NearQuery.near(point).maxDistance(1000).spherical(true);
  Aggregation agg = Aggregation.newAggregation(Aggregation.geoNear(nearQuery, "calculated"),Aggregation.unwind("items"),
    Aggregation.group("merchantId", "_id", "catalogTypeId", "catalogStatusId", "departmentName",
                        "categoryName", "subCategoryName", "serviceFlag", "date", "availability", "distance",
                        "commissionPercentage", "createdBy", "updatedBy", "departmentId", "categoryId",
                        "subCategoryId", "createdOn", "updatedOn").push("items").as("items"),
                Aggregation.skip(skip), Aggregation.limit(limit)

        );

can someone help me find out how to run this request in java

my document structure

{      "_id": "CAT106679778",      "_class": "com.ikarma.core.catalog.domain.CatalogForAdminAndMerchant",      "merchantId": "M117442443",      "catalogTypeId": "catalogTypeProduct",      "catalogStatusId": "catalogStatusDraft",      "": [         {              "" : " " ,              "" : " " ,              "" : "1000" ,              "itemStatusId" : "catalogStatusDraft",              "itemStatusName" : "" ,              "unitMeasure" : 30,              "" : NumberInt ( "12" ),              "availableStock" : NumberInt ( "12" ),              "orderStock" : NumberInt ( "0" ),              "" : [                  " https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary JewelryJewelry/1520415128722cheers.jpg"             ],              "activeFlag" : "undefined" ,              "tags" : [                  " " ,                  " "             ],              "" : "25" ,              " " : "10" ,              "unitName" : "Gm" ,              "itemId" : NumberLong ( "78921671000" ),              "" : [                 ,                 ,                              ],              "CommissionPercentage" : 10,              "CommissionAmount" : 0,              "itemIsDeleted" : "false" ,              "netSellingPrice" : 750,              "starCount" : 0,              "deliveryCharges" : 0,              "customItemCode" : "zdfg" ,              "" : 0,              "" : ,              "itemsLat" : 21,              "itemsLong" : 74,              "ratingAvg" : 0,              "" : {                  "type" : "Point" ,                  "" : [                     74,                     21                 ]             }         },         {              "" : " " ,              "" : " " ,              "" : "1000" ,              "itemStatusId" : "catalogStatusDraft",              "itemStatusName" : "" ,              "unitMeasure" : 30,              "" : NumberInt ( "12" ),              "availableStock" : NumberInt ( "12" ),              "orderStock" : NumberInt ( "0" ),              "" : [                  " https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary JewelryJewelry/1520415128722cheers.jpg"             ],              "activeFlag" : "undefined" ,              "tags" : [                  " " ,                  " "             ],              "" : "25" ,              " " : "10" ,              "unitName" : "Gm" ,              "itemId" : NumberLong ( "1347268704" ),              "" : [                 ,                 ,                              ],              "CommissionPercentage" : 10,              "CommissionAmount" : 0,              "itemIsDeleted" : "false" ,              "netSellingPrice" : 750,              "starCount" : 0,              "deliveryCharges" : 0,              "customItemCode" : "zdfg" ,              "" : 0,              "" : ,              "itemsLat" : 22,              "itemsLong" : 75,              "ratingAvg" : 0,              "" : {                  "type" : "Point" ,                  "" : [                     75,                     22                 ]             }         },         {              "" : " " ,              "" : " " ,              "" : "1000" ,              "itemStatusId" : "catalogStatusDraft",              "itemStatusName" : "" ,              "unitMeasure" : 30,              "" : NumberInt ( "12" ),              "availableStock" : NumberInt ( "12" ),              "orderStock" : NumberInt ( "0" ),              "" : [                  " https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversaryGiftsJewelryJewelry/1520415128722cheers.jpg"             ],              "activeFlag" : "undefined" ,              "tags" : [                  " " ,                  " "             ],              "" : "25" ,              " " : "10" ,              "unitName" : "Gm" ,              "itemId" : NumberLong ( "10043410600" ),              "" : [                 ,                 ,                              ],              "CommissionPercentage" : 10,              "CommissionAmount" : 0,              "itemIsDeleted" : "false" ,              "netSellingPrice" : 750,              "starCount" : 0,              "deliveryCharges" : 0,              "customItemCode" : "zdfg" ,              "" : 0,              "" : ,              "itemsLat" : 23,              "itemsLong" : 76,              "ratingAvg" : 0,              "" : {                  "type" : "Point" ,                  "" : [                     76,                     23                 ]             }         }     ],      "departmentName": "",      "categoryName": " ",      "subCategoryName": " " ,      "serviceFlag": ",     " updatedBy": [         {              "_id": "M117442443",             "name ":" Sales Team "        }     ],     " ": [         {              "_id": "M117442443",             "name ":" Sales Team "        }     ],     " departmentId ":" 5948c14be4b0d3372e47423d ",     " categoryId ":" 5948cc1fe4b0d3372e474287 ",     " catalogIsDeleted": "false" ,     "subCategoryId ":" 5948cd46e4b0d3372e474291 ",     " createdOn ": ISODate (" 2018-03-07T15: 03: 34.877 + 05: 30 "),     " updatedOn ": ISODate (" 2018-03-08T13: 09: 53.373 + 05: 30 "),     " ": [         {             " startTime ":" null ",             " endTime ":" null "        },         {             " startTime ":" null ",             " endTime ":" null "        },         {             " startTime ":" null ",             " endTime ":" null "        }     ],     " ": "0" ,     "merchantStatus ":" "        }

+4
1

AggregationOperation $redact.

1.8.5.

AggregationOperation redactOperation = aggregationOperationContext -> {
    Map<String, Object> map = new LinkedHashMap<>();
    BasicDBObject cmp =  new BasicDBObject("$cmp", Arrays.asList( "$items.address","$dist.location"));
    map.put("if", new BasicDBObject("$eq", Arrays.asList(cmp, 0)));
    map.put("then", "$$KEEP");
    map.put("else", "$$PRUNE");
    return new BasicDBObject("$redact", new BasicDBObject("$cond", map));
};

Aggregation agg = Aggregation.newAggregation(
    Aggregation.geoNear(nearQuery, "calculated"), 
    Aggregation.unwind("items"),
    redactOperation,
    Aggregation.skip(skip), 
    Aggregation.limit(limit)
);
+1

Source: https://habr.com/ru/post/1694894/


All Articles