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

时间:2018-03-15 05:01:26

标签: java mongodb

我无法使用mongodb模板添加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"
 } 
} 
}
])

这是我的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)

        );

任何人都可以帮我找到如何在java中运行此查询

我的文档结构

  

{       " _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 GiftsJewelryJewelry / 1520415128722cheers.jpg"               ]               " activeFlag" :" undefined",               "标记" :[                   "珠宝&#34 ;,                   "珠宝"               ]               "折扣" :" 25",               " advancePayment" :" 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,               "地址" :{                   "类型" :" 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 GiftsJewelryJewelry / 1520415128722cheers.jpg"               ]               " activeFlag" :" undefined",               "标记" :[                   "珠宝&#34 ;,                   "珠宝"               ]               "折扣" :" 25",               " advancePayment" :" 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,               "地址" :{                   "类型" :" 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/GiftsAnniversary GiftsJewelryJewelry / 1520415128722cheers.jpg"               ]               " activeFlag" :" undefined",               "标记" :[                   "珠宝&#34 ;,                   "珠宝"               ]               "折扣" :" 25",               " advancePayment" :" 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,               "地址" :{                   "类型" :" Point",                   "坐标" :[                       76,                       23                   ]               }           }       ]       " DEPARTMENTNAME" :" Gifts",       "类别名称" :"周年纪念礼品",       " subCategoryName" :"珠宝",       " serviceFlag" :"",       " updatedBy" :[           {               " _id" :" M117442443",               "名称" :"销售团队"           }       ]       " createdBy" :[           {               " _id" :" M117442443",               "名称" :"销售团队"           }       ]       " 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",               "结束时间" :" null"           },           {               " STARTTIME" :" null",               "结束时间" :" null"           },           {               " STARTTIME" :" null",               "结束时间" :" null"           }       ]       "距离" :" 0",       " merchantStatus" :"接受"          }

谢谢

1 个答案:

答案 0 :(得分:3)

您可以使用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)
);