I have the following collections in my MongoDB (v.4.4):
guilds:
{
_id: String,
members: [{ _id String, rank: number },{ _id String, rank: number }...]
}
and characters:
{
_id: String,
many_other: 'fields'
}
And I'd like to join with the $lookup pipeline syntax guild.members <= characters by _id field.
I don't use Mongo build-in
OjbectIdsas_id, I overwrite it with strings in both collections. As I heard, there is a bit different behaviour with$lookupwith strings as_idin pipelines. So make sure, that you knew about it, before answering.
The expected result that I want is simple, I'd like to save rank from the original document, and also add any other field from $lookup it:
{
_id: String // (guild)
members: [
{
_id: String, // (characters)
rank: number,
many_other: '...fields from characters'
},
{
_id: String, // (characters)
rank: number,
many_other: '...fields from characters'
}, ...
]
Mongo Playground example: avaliable
What have I tried:
Various queries, like:
{
$lookup: {
from: "characters",
pipeline: [
{
$match: {
$expr: { $eq: [ { $toString :"$members._id" }, { $toString : "$$character_id" } ] }
}
}
],
as: "guild_members"
},
}
With converting ID's to string, with let stages variables, and using $map operator. But I still far away from the required result.
There is also another problem, which is some relevance to the question, but not related to queries itself
As you may see, the characters collection has many other fields. Some of them are pretty heavy, (because guilds can have up to 500 members) which making the result document >16 MB (MongoDB threshold limit) which gives an error. Since, as far as I know, we could not pick fields from joined documents, it will be much better to exclude it right after the $lookup stage or something like that. Any advice about it will be pretty welcome.