如何在Spring-Data中使用MongoDB $ let?

时间:2016-07-12 11:59:39

标签: java spring mongodb spring-data aggregation-framework

我有一个MongoDB的地方集合。典型的地方有以下大部分字段:

{
"_id" : ObjectId("575014dc6b028f07bef53681"),
"_class" : "domain.model.PlaceV1",
"name" : "Γιασεμί",
"primary_photo_url" : "https://irs0.4sqi.net/img/general/original/34666238_STHSh6CHiC7hpAuB4rztRVg6cFc5ylfi15aRaR7zUuQ.jpg",
"seenDetails" : NumberLong(0),
"foursquare_checkins" : 646,
"foursquare_tips" : 28,
"keywords" : [ 
    ""
],
"verified" : 1,
"location" : {
    "loc" : {
        "type" : "Point",
        "coordinates" : [ 
            25.898318, 
            36.831486
        ]
    },
    "formattedAddress" : "Χώρα",
    "locality" : "Amorgos",
    "first_neighbourhood" : "Katapola",
    "greek_locality" : "Αμοργός",
    "greek_first_neighbourhood" : "Κατάπολα"
},
"contact" : {
    "phone_numbers" : [ 
        "+30 2285 074017"
    ]
},
"price" : {
    "priceVotes" : NumberLong(0),
    "price" : 0,
    "priceVotesSum" : NumberLong(0)
},
"rating" : {
    "rating" : 8,
    "ratingVotes" : NumberLong(0),
    "ratingVotesSum" : NumberLong(0)
},
"categories" : [ 
    {
        "cat_id" : NumberLong(10310061000),
        "category" : "Café",
        "greek_category" : "Καφετέρια",
        "weight" : 4
    }, 
    {
        "cat_id" : NumberLong(11610021000),
        "category" : "Bar",
        "greek_category" : "Μπαρ",
        "weight" : 4
    }
]
}

我想进行查询,其中排序将基于某些表达式和条件的结果。从mongo shell我试过这个:

db.place.aggregate([
{$match:{"location.locality":"Athens"}}, 
{$project:
    {name:1, location:1, score:{                  
            $let: {                    
                 vars:{ foursquare: {
                                     $cond: { if: { $gte: [ "$foursquare_checkins", 500 ] }, then: 500, else: "$foursquare_checkins" }
                                    },
                        rating: {$multiply:["$rating.rating", 100]},
                        },
                 in:{$add:["$$foursquare", "$$rating", "$seenDetails"]}            
                }          
       }                 
    }
},
{$sort: {score: -1}}]).pretty();

这是我查询的一个简单示例。分数将包含更复杂的表达式,例如距离某个位置的距离。问题是我找不到在Spring的Java代码中使用$ let和$ cond运算符的方法。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您应该可以使用嵌套的DBObject和自定义聚合操作来执行此操作。

例如:

Map operations = new HashMap();
operations.put("name", 1);
operations.put("location", 1);
operations.put("score", new BasicDBObject("$let", new BasicDBObject("vars", new BasicDBObject())));

然后,您可以创建CustomAggregationOperation将其添加到项目中

CustomAggregationOperation project = new CustomAggregationOperation(new BasicDBObject("$project", operation));

这将为您提供以下管道:

{ "$project" : { "score" : { "$let" : { "vars" : { }}} , "name" : 1 , "location" : 1}}

然后你可以添加你的其他阶段:

Aggregation aggregate = Aggregation.newAggregation(match, project, sort);
public class CustomAggregationOperation implements AggregationOperation {
    private DBObject operation;

    public CustomAggregationOperation (DBObject operation) {
        this.operation = operation;
    }

    @Override
    public DBObject toDBObject(AggregationOperationContext context) {
        return context.getMappedObject(operation);
    }
}