'MongoDB Atlas Search index on normalized/indexed model

I'd like to use the fresh Atlas search index feature to perform search through my models.

It seems to me that the data model that I used can't be coupled with this mongo feature. It seems to work really fine on embedded models, but for consistency reasons I can't nest objects, they are referenced by their id.

Example

Collection Product

{
  name: "Foo product"
  quantity: 3
  tags: [
    "id_123"
  ]
}

Collection Vendor

{
  name: "Bar vendor"
  address: ...
  tags: [
    "id_123"
  ]
}

Collection Tags

{
  id: "id_123"
  name: "food"
}

What I want

I want to type food in my search bar, and find the products associated to the tag food.

Detailed problematic

I have multiple business objects that are labelled by the same tag. I'd like to build a search index to search through my products, but I would want to $lookup before to denormalize my ids and to be able to find all the products that have the tag "food".

From the documentation, the $search operator must be the first operator of the aggregation pipeline, preventing me from lookup before searching. I had the idea to build a view first, to unpack the id with the correct tag to prepare the field. But impossible to build a search index on a view.

Is it completely impossible to make this work ? Do I need to give up on consistency on my tags by flattening and by embedding each of them directly in each model I need them to be able to use this feature ? That means if I want to update a tag, I need to find every business object that carry around the tag, and perform the update ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source