Как раскрутить и сгруппировать в Java для коллекций Mongodb - PullRequest
0 голосов
/ 05 октября 2019

хочу сделать раскрутку и сгруппировать по одной коллекции. Я могу сделать это в Mongodb Cli, но не могу сделать в Java.

Ниже моя команда

db.tasks.aggregate([ {$match:{_id : ObjectId("5d7f70301498b60e64515e76")}},  { "$lookup": { "from":"users","localField":"approver","foreignField":"_id", "as":"approver_info"} },{"$unwind":"$approver_info"} , { "$lookup": { "from":"projects", "localField":"projectId", "foreignField":"_id", "as":"project_info"} },  {"$unwind":"$project_info"}, {"$unwind":"$volunteers"}, { "$lookup": { "from":"users", "localField":"volunteers", "foreignField":"_id", "as":"volunteers_info"} } , {$unwind : "$volunteers_info"}, {$group : { "_id" : "$_id","name" :{"$first":"$name"}, "desc":{"$first" : "$description"}, "vol_info":{$push : "$volunteers_info"} }}])

С вышеприведенной командой, я вышел, как показано ниже:

>
{ "_id" : ObjectId("5d7f70301498b60e64515e76"), "name" : "TASK1", "desc" : "Task Description", "vol_info" : [ { "_id" : ObjectId("5d8d1e59ce6b3806fce8ce30"), "user_id" : "abc@tip.com", "name" : "abc", "email" : "abc@tip.com", "phone_no" : "123456789", "is_stakeholder" : "Y", "_class" : "benefitBountyService.models.User" }, { "_id" : ObjectId("5d8d1e59ce6b3806fce8ce31"), "user_id" : "poc1@tip.com", "name" : "POC1", "email" : "poc1@tip.com", "phone_no" : "789456123", "_class" : "benefitBountyService.models.User" } ] }
>

В Java, я пытался сделать ниже:

AggregateIterable<Document> output = taskCollection.aggregate(
 Arrays.asList(
 Aggregates.match(Filters.eq("_id",new ObjectId(taskId))),
 Aggregates.lookup("users","approver", "_id", "approver_info"),
 Aggregates.unwind("$approver_info"),
 Aggregates.lookup("projects","projectId", "_id", "project_info"),
 Aggregates.unwind("$project_info"),
 Aggregates.unwind("$volunteers"),
 Aggregates.lookup("users","volunteers", "_id", "volunteers_info"),
 Aggregates.unwind("$volunteers_info"),
 //Aggregates.group("$_id",Accumulators.push("vols_info","$volunteers_info"))

 )
 );

Но это не дает мне результат, как показано ниже. Не могли бы вы помочь мне с этим?

...