Mongo text search aggregation for nested array of objects

Viewed 121

I have a document with a nested array of objects with a long list of properties. Instead of searching through the documents, I need a search inside one document's nested data.

(nested properties have indexes like 'data.$**': 'text')

Document item:

    "_id": "...",
    "data": [
         {
            "name": "Bob"
            ... 
         },
         {
            "name": "Mark" 
            ...
         },
         {
            "name": "John"
            ... 
         }
      ]
    }

Some wrong example of code for explaining what I want (cause error '...$text is only allowed...')

   const foundData = await this.fileModel
      .aggregate(
        [
          { $unwind: '$data' },
          { $match: { $text: { $search: "Mark" } } },
        ])
      .exec();

Without $unwind it will work of course but will return whole objects in array.

Needed result

    "_id": "...",
    "data": [
         {
            "name": "Mark" 
            ...
         }
      ]
    }

P.S: Solutions like this and this is not ok, since I don't want listed hundreds of properties...

0 Answers
Related