Saturday 8 April 2017

Mogodb Aggregation

this is my user collection

{ 
    "_id" : ObjectId("58e8cb640f861e6c40627a06"), 
    "actorId" : "665991", 
    "login" : "petroav", 
    "gravatar_id" : "", 
    "url" : "https://api.github.com/users/petroav", 
    "avatar_url" : "https://avatars.githubusercontent.com/u/665991?" 

}

this is my repo collection

{ 
    "_id" : ObjectId("58e8cb640f861e6c40627a07"), 
    "repoId" : "28688495", 
    "name" : "petroav/6.828", 
    "url" : "https://api.github.com/repos/petroav/6.828"
}

this is my events collections

{ 
    "_id" : ObjectId("58e8cb640f861e6c40627a08"), 
    "eventId" : "2489651045", 
    "type" : "CreateEvent", 
    "actorLogin" : "petroav", 
    "repoId" : "28688495", 
    "eventDate" : ISODate("2015-01-01T15:00:00.000+0000"), 
    "public" : true         
}

i am trying to do following queries on above data

  1. Return list of all repositories with their top contributor
  2. Find the repository with the highest number of events from an actor (by login). If multiple repos have the same number of events, return the one with the latest event.
  3. Return actor details and list of contributed repositories by login

i tried 3 one by doing this

db.events.aggregate(
   [    {
            $match:{"actorLogin":"petroav"}
        },
        {
            $lookup:{
                from:"repos",
                localField:"repoId",
                foreignField:"repoId",
                as:"Repostory"
                }
        },
        {
            $group:{ _id : "$Repostory", repo: { $push: "$$ROOT" } } 
        }

   ]
).pretty()

please help i new to mongodb



via Mir Rayees Ahmad

No comments:

Post a Comment