I'm trying to make a MongoDB Database with a few collections. These collections have a one-to-many relationship with eachother, in the sense that:
- One {A} has (0..*) of {B}
- One {B} has (0..*) of {C}
- ... and a few levels deep.
As per structure, I've got a very basic structure wherein
- "_id" is the object's own ID
- "Aid" and "Bid" for respectively {B} and {C} is the parent's ID.
- There cannot be a {B} whose Aid doesn't match any {A}'s _id.
- Any other object fields are stored separately.
Everything is an integer that is auto-incremented clientside. I can just as easily check clientside if there is an {A} whose _id matches {B}'s Aid, but I'm curious if I can use $lookup and $in serverside too. For this purpose, performance will not be terribly important as the amount of {C}'s will probably not pass 100k.
However, there exists an object {D} that will have a huge amount of objects inserted over its lifetime (say, a few billion each year), so if a $lookup is possible but performance-wise not feasible, I can live with doing validation for {D} clientside.
Problem:
I've got the following query for {B} that's failing:
db.createCollection("B", {
validator: { $and :
[
{ $jsonSchema: {
bsonType: "object",
required: ["_id", "Aid"],
properties: {
_id: {
bsonType: "long",
description: "Bid must be a number and is required!"
},
Aid: {
bsonType: "long",
description: "Aid must be a number and is required!"
}
}
} },
{
Aid: {
$in : [
{ $lookup : {
from : "A",
localField : "Aid",
foreignField : "_id",
as : "A_IDs"
} }
]
}
}
]
}
});
Error given:
{
"ok" : 0,
"errmsg" : "cannot nest $ under $in",
"code" : 2,
"codeName" : "BadValue"
}
I understand that the above query is failing because a $in
cannot work with the $lookup
as presented above. However, what I'm asking is if the idea is possible at all, and if so then how.