user schema:
{
_id: "OjectId",
}
interaction schema:
{
blocker_id: user_id_x
blocked: {
user_id_y: true,
user_id_z: true,
etc...
}
}
db.user.aggregate([{
$lookup: {
from: "interaction",
as: "remove",
let: { tar_id: "$_id" },
pipeline: [
{
$match: {
[`blocked[$$tar_id]`]: true,
},
},
{
$limit: 1,
},
{
$project: {
_id: 0,
remove: "true",
},
},
],
},
}]}
Notice the line:
[`blocked[$$tar_id]`]: true,
Does the above lookup work? If not, how can I get it to work as intended?
The reason for this schema design is because looking up the value of an object’s property is O(1), while looking up an element of an array is O(N). Huge difference. Any solution must maintain the O(1) time complexity of the query.
EDIT:
tested. It does not work. The problem remains, how do I get to work as desired?
One option is:
See how it works on the playground example