5

I have this query that provides me the join I want to:

db.summoners.aggregate([
    { "$match": { "nick":"Luispfj" } },
    { "$unwind": "$matches" },
    {
        "$lookup": {
            "from":"matches",
            "localField":"matches.gameId",
            "foreignField":"gameId",
            "as":"fullMatches"
        }
    },
    { "$unwind": "$fullMatches" },
    {
        "$group": {
            "_id": null,
            "matches": { "$push":"$fullMatches" }
        }
    }
])

But when I run the unwind function the null entries are gone. How do I retrieve them (with their respective "gameId"s, if possible?

Also, is there a way to retrieve only the matches array, instead of it being a subproperty of the "null-id-object" it creates?

Luis Paulo
  • 129
  • 2
  • 10

1 Answers1

11

$unwind takes an optional field preserveNullAndEmptyArrays which by default is false. If you set it to true, unwind will output the documents that are null. Read more about $unwind

{ 
  "$unwind": {
    path: "$fullMatches",
    preserveNullAndEmptyArrays: true
  }
},
Stubbies
  • 3,054
  • 1
  • 24
  • 33