Perform a search over multiple collections
Right now you can only search over one collection. I'd like to be able to search over several collections at the same time.
Leverage $unionWith and $lookup to run search queries against multiple collections or create a materialized view prior to indexing. View tutorials here.
-
AdminMarcus Eagan (Admin, MongoDB) commented
Starting in MongoDB 4.2, you can search over multiple collections with materialized views. For some use cases that is not idea, but for many it should work just fine. If it won't work in your case, please let us know why it would not.
-
Tim Frietas commented
i want to use searchBeta operator on dbref objects attribute how i can give instructions to analyzer to access fields from reference object and do indexing
For example, assuming the following two collections:
Collection1
{
_id: ObjectId("5d636ff2259c5a0b3c83d2f1"),
name: "This",
something: "Else",
emp: ObjectId("5d639e2af8e23f1fb0921032")
}Collection2
{
_id: ObjectId("5d639e2af8e23f1fb0921032"),
name: "ABC"
salary: 1000
}I want to be able to run a query like the following:
db.Collection1.aggregate([
{
$searchBeta: {
"term": {
"query": "ABC",
"path": "emp.name"
}
}
}
])