2014-01-10 104 views
2

爲了使MongoDB使用Spring Data/Java API,我如何將下面的mongo shell查詢轉換爲Spring Data/Java查詢,任何幫助都很棒......將Mongo Shell腳本轉換爲SpringData/Java

> db.collection.aggregate(
{$unwind : "$data.mi"}, 
{$sort : {"data.mi.failures" : -1}}, 
{$group : {_id : "$data.mi.name", 
      sum_total_of_failures : {$sum : "$data.mi.failures"}, 
      uid : {$first : "$uid"}}} 
) 

回答

2

在春天,它看起來像這樣

String collection="collection"; 

    Aggregation agg = newAggregation(
      unwind("data.mi") 
      ,sort(DESC,"data.mi.failures") 
      ,group("data.mi.name") 
      .sum("data.mi.failures").as("sum_total_of_failures") 
      .first("uid").as("uid") 
    ); 
    AggregationResults<Type> aggregate = mongoOperations.aggregate(agg, collection, objectClass); 

隨着這些進口假設

import com.mongodb.BasicDBObject; 
import org.springframework.data.mongodb.core.MongoOperations; 
import org.springframework.data.mongodb.core.aggregation.Aggregation; 
import org.springframework.data.mongodb.core.aggregation.AggregationResults; 
import org.springframework.data.mongodb.core.query.Criteria; 
import org.springframework.data.mongodb.core.query.Query; 

import static org.springframework.data.domain.Sort.Direction.*; 
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; 
+0

謝謝,這是有益的... :) – vmr

+0

好 - 如果你快樂,你能接受答案嗎? :-) –

+0

我是................ :) – vmr