2016-07-12 6 views
0

私は場所の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シェルから私はこれを試しました:

これは私のクエリの簡単な例です。スコアには、場所からの距離など、より複雑な表現が含まれます。問題は、SpringでJavaコードで$ letと$ cond演算子を使用する方法が見つからないことです。誰も助けてくれますか?

答えて

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 project = new CustomAggregationOperation(new BasicDBObject("$project", operation)); 

プロジェクトにこれを追加するCustomAggregationOperation作成することができます。

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

をその後、他の駅を追加することができますGES:

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); 
    } 
} 
関連する問題