2017-02-13 4 views
1

MongoDB $group schreiben:wie Kriterien Abfrage für MongoDB Gruppe Aggregation

db.careLogBean.aggregate([{ 
    $unwind: "$comments" 
}, { 
    $sort: { 
     "comments.time": -1 
    } 
}, { 
    $group: { 
     _id: "$_id", 
     careGiverId: { 
      $first: "$careGiverId" 
     }, 
     careGiverName: { 
      $first: "$careGiverName" 
     }, 
     comments: { 
      $push: "$comments" 
     } 
    } 
}]) 

In mongodb $group aggretion Dinge ... wie in Java-Kriterien Abfragesprache ...

+0

Können Sie bitte zeigen, was Sie bisher versucht haben? – Veeram

+0

Sorry für die späte Wiedergabe veeram .. –

+0

DBObject group = neu BasicDBObject ("$ group", neues BasicDBObject ("_ id", "$ name", "$ Gehalt")); –

Antwort

1

Sie können versuchen, zu schreiben, die unter Aggregation.

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

Aggregation aggregation = newAggregation(
     unwind("comments"), 
     sort(DESC, "comments.time"), 
     group("_id") 
      .first("careGiverId").as("careGiverId") 
      .first("careGiverName").as("careGiverName") 
      .push("comments").as("comments")); 

List<BasicDBObject> dbObjects = mongoOperations.aggregate(aggregation , collection_name, BasicDBObject.class).getMappedResults(); 

Sie können die letzte Aussage basierend auf Ihrer Einrichtung anpassen.

+0

danke Veeram ... –

+0

Ich überprüfe jetzt .. Sie wissen, was passiert ist –

+0

ha, es funktioniert gut ..veeram –

Verwandte Themen