简体   繁体   English

如何在Java中使用带有mongodb模板的$ redact和Aggregation

[英]How to use $redact with Aggregation in Java with mongodb template

I'm unable to add redact with mongodb template below is my mongo code which is working fine and using sping mongo 1.8.5 我无法在下面添加mactodb模板的redact是我的mongo代码,它正常工作并使用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 which is not working as above mongo code 这是我的java代码,它不像上面的mongo代码那样工作

 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 anybody help me to find out how to run this query in java 任何人都可以帮我找到如何在java中运行此查询

my document structure 我的文件结构

{ "_id" : "CAT106679778", "_class" : "com.ikarma.core.catalog.domain.CatalogForAdminAndMerchant", "merchantId" : "M117442443", "catalogTypeId" : "catalogTypeProduct", "catalogStatusId" : "catalogStatusDraft", "items" : [ { "name" : "Jewelry", "description" : "Jewelry", "price" : "1000", "itemStatusId" : "catalogStatusDraft", "itemStatusName" : "Draft", "unitMeasure" : 30, "stock" : NumberInt("12"), "availableStock" : NumberInt("12"), "orderStock" : NumberInt("0"), "photos" : [ " https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary GiftsJewelryJewelry/1520415128722cheers.jpg" ], "activeFlag" : "undefined", "tags" : [ "Jewelry", "Jewelry" ], "discount" : "25", "advancePayment" : "10", "unitName" : "Gm", "itemId" : NumberLong("78921671000"), "videos" : [ null, null, null ], "commissionPercentage" : 10, "commissionAmount" : 0, "itemIsDeleted" : "false", "netSellingPrice" : 750, "starCount" : 0, "deliveryCharges" : 0, "customItemCode" : "zdfg" {“_ id”:“CAT106679778”,“_ class”:“com.ikarma.core.catalog.domain.CatalogForAdminAndMerchant”,“merchantId”:“M117442443”,“catalogTypeId”:“catalogTypeProduct”,“catalogStatusId”:“catalogStatusDraft” ,“items”:[{“name”:“Jewelry”,“description”:“Jewelry”,“price”:“1000”,“itemStatusId”:“catalogStatusDraft”,“itemStatusName”:“Draft”,“unitMeasure” :30,“stock”:NumberInt(“12”),“availableStock”:NumberInt(“12”),“orderStock”:NumberInt(“0”),“photos”:[“ https://clappilystorage.blob。 core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary GiftsJewelryJewelry / 1520415128722cheers.jpg“],”activeFlag“:”undefined“,”tags“:[”Jewelry“,”Jewelry“],”discount“:”25“ ,“advancePayment”:“10”,“unitName”:“Gm”,“itemId”:NumberLong(“78921671000”),“videos”:[null,null,null],“commissionPercentage”:10,“commissionAmount”: 0,“itemIsDeleted”:“false”,“netSellingPrice”:750,“starCount”:0,“deliveryCharges”:0,“customItemCode”:“zdfg” , "taxes" : 0, "perishable" : false, "itemsLat" : 21, "itemsLong" : 74, "ratingAvg" : 0, "address" : { "type" : "Point", "coordinates" : [ 74, 21 ] } }, { "name" : "Jewelry", "description" : "Jewelry", "price" : "1000", "itemStatusId" : "catalogStatusDraft", "itemStatusName" : "Draft", "unitMeasure" : 30, "stock" : NumberInt("12"), "availableStock" : NumberInt("12"), "orderStock" : NumberInt("0"), "photos" : [ " https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary GiftsJewelryJewelry/1520415128722cheers.jpg" ], "activeFlag" : "undefined", "tags" : [ "Jewelry", "Jewelry" ], "discount" : "25", "advancePayment" : "10", "unitName" : "Gm", "itemId" : NumberLong("1347268704"), "videos" : [ null, null, null ], "commissionPercentage" : 10, "commissionAmount" : 0, "itemIsDeleted" : "false", "netSellingPrice" : 750, "starCount" : 0, "deliveryCharges" : 0, "customItemCode" : "zdfg", "taxes" : 0, "perishable" : false, "itemsLat" : 22, "itemsLong" : 7 ,“tax”:0,“易腐”:false,“itemsLat”:21,“itemsLong”:74,“ratingAvg”:0,“address”:{“type”:“Point”,“coordinates”:[74 ,21,}},{“name”:“Jewelry”,“description”:“Jewelry”,“price”:“1000”,“itemStatusId”:“catalogStatusDraft”,“itemStatusName”:“Draft”,“unitMeasure” :30,“stock”:NumberInt(“12”),“availableStock”:NumberInt(“12”),“orderStock”:NumberInt(“0”),“photos”:[“ https://clappilystorage.blob。 core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary GiftsJewelryJewelry / 1520415128722cheers.jpg“],”activeFlag“:”undefined“,”tags“:[”Jewelry“,”Jewelry“],”discount“:”25“ ,“advancePayment”:“10”,“unitName”:“Gm”,“itemId”:NumberLong(“1347268704”),“videos”:[null,null,null],“commissionPercentage”:10,“commissionAmount”: 0,“itemIsDeleted”:“false”,“netSellingPrice”:750,“starCount”:0,“deliveryCharges”:0,“customItemCode”:“zdfg”,“tax”:0,“易腐”:false,“itemsLat “:22,”itemsLong“:7 5, "ratingAvg" : 0, "address" : { "type" : "Point", "coordinates" : [ 75, 22 ] } }, { "name" : "Jewelry", "description" : "Jewelry", "price" : "1000", "itemStatusId" : "catalogStatusDraft", "itemStatusName" : "Draft", "unitMeasure" : 30, "stock" : NumberInt("12"), "availableStock" : NumberInt("12"), "orderStock" : NumberInt("0"), "photos" : [ " https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary GiftsJewelryJewelry/1520415128722cheers.jpg" ], "activeFlag" : "undefined", "tags" : [ "Jewelry", "Jewelry" ], "discount" : "25", "advancePayment" : "10", "unitName" : "Gm", "itemId" : NumberLong("10043410600"), "videos" : [ null, null, null ], "commissionPercentage" : 10, "commissionAmount" : 0, "itemIsDeleted" : "false", "netSellingPrice" : 750, "starCount" : 0, "deliveryCharges" : 0, "customItemCode" : "zdfg", "taxes" : 0, "perishable" : false, "itemsLat" : 23, "itemsLong" : 76, "ratingAvg" : 0, "address" : { "type" : "Point", "coordinates" : 5,“ratingAvg”:0,“地址”:{“type”:“Point”,“coordinates”:[75,22]}},{“name”:“Jewelry”,“description”:“Jewelry”, “price”:“1000”,“itemStatusId”:“catalogStatusDraft”,“itemStatusName”:“Draft”,“unitMeasure”:30,“stock”:NumberInt(“12”),“availableStock”:NumberInt(“12” ),“orderStock”:NumberInt(“0”),“photos”:[“ https://clappilystorage.blob.core.windows.net/clappilymerchant/M117442443/MyProduct/GiftsAnniversary GiftsJewelryJewelry / 1520415128722cheers.jpg”],“activeFlag “:”undefined“,”tags“:[”Jewelry“,”Jewelry“],”discount“:”25“,”advancePayment“:”10“,”unitName“:”Gm“,”itemId“:NumberLong( “10043410600”),“videos”:[null,null,null],“commissionPercentage”:10,“commissionAmount”:0,“itemIsDeleted”:“false”,“netSellingPrice”:750,“starCount”:0,“ deliveryCharges“:0,”customItemCode“:”zdfg“,”tax“:0,”perishable“:false,”itemsLat“:23,”itemsLong“:76,”ratingAvg“:0,”address“:{”type “:”点“,”坐标“: [ 76, 23 ] } } ], "departmentName" : "Gifts", "categoryName" : "Anniversary Gifts", "subCategoryName" : "Jewelry", "serviceFlag" : "", "updatedBy" : [ { "_id" : "M117442443", "name" : "Sales Team" } ], "createdBy" : [ { "_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"), "availability" : [ { "startTime" : "null", "endTime" : "null" }, { "startTime" : "null", "endTime" : "null" }, { "startTime" : "null", "endTime" : "null" } ], "distance" : "0", "merchantStatus" : "Accepted" } [76,23]}}],“departmentName”:“Gifts”,“categoryName”:“Anniversary Gifts”,“subCategoryName”:“Jewelry”,“serviceFlag”:“”,“updatedBy”:[{“_ id” :“M117442443”,“name”:“Sales Team”}],“createdBy”:[{“_ 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”:“已接受”}

thank you 谢谢

You can use AggregationOperation to build $redact aggregation stage. 您可以使用AggregationOperation来构建$redact聚合阶段。

You will need 1.8.5 version. 你需要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)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM