for example i have 3 collection:
user:
db.users.insert({ _id: 1, username: ‘mark’ });
db.users.insert({ _id: 2, username: ‘leon’ });
db.users.insert({ _id: 3, username: ‘leontin’});
providers:
db.providers.insert({ _id: 1, name: ‘prov1’ });
db.providers.insert({ _id: 2, name: ‘prov2’ });
db.providers.insert({ _id: 3, name: ‘prov3’ });
db.providers.insert({ _id: 39, name: ‘prov39’ });
data:
db.data.insert({
_id: ObjectId(‘6675309f82bdfbe3b00febfa’),
user: 1,
provider: 1,
message: ‘94451 Laos Sweden Iran’,
});
db.data.insert({
_id: ObjectId(‘6675309f82bdfbe3b00febfb’),
user: 2,
provider: 3,
message: ‘message 22222’,
});
db.data.insert({
_id: ObjectId(‘6675309f82bdfbe3b00febfc’),
user: 3,
provider: 39,
message: ‘message 3333’,
});
P.S. i have more 2 related collection with ‘date’ but i write the example only with 2 related (normally are 4 collections)
i search ‘2 likes’ … over 3.200.000 rows in data, 20.000 users, 10000 providers
db.data.aggregate([
{
“$lookup”: {
“from”: “users”,
“localField”: “user”,
“foreignField”: “_id”,
“as”: “user_info”
}
},
{
“$lookup”: {
“from”: “providers”,
“localField”: “provider”,
“foreignField”: “_id”,
“as”: “provider_info”
}
},
{
$match: {
$expr: {
$and: [
{
$regexMatch: {
input: {
$first: “$user_info.username”
},
regex: “^leo”,
options: “i”
}
},
{
$regexMatch: {
input: {
$first: “$provider_info.name”
},
regex: “^prov3”,
options: “i”
}
}
]
}
}
}
])
example query: Mongo playground
the result are about 15+ seconds and i need over 100.000.000 in data collections (users and providers are ok)
i have index in data.user, data.provider, users.username, providers.name
how to improve this relations ? thinking that the 4 collections will constantly change the data (users.username, providers,name, …, …)
Regards, IATN