I have a series of documents in MongoDB that look like this:
{ "playerId" : ObjectId("5c58363b5c226c24b0b37860"), "gameId" : ObjectId("5c59697f57ef0f512c1cb228"), "state" : 4 } { "playerId" : ObjectId("5beab425c0d75e5afabc1638"), "gameId" : ObjectId("5c59697f57ef0f512c1cb228"), "state" : 4 } I would like to aggregate and have the below result:
{ "_id" : ObjectId("5beab425c0d75e5afabc1638"), // the playerId "state" : 4, "opponents": ["","","",""] // all the opponents gameId } Group them by the playerId, find all games, user is playing and get all the opponents playerId.
Any thoughts?