I have a Mongodb doc structure, with many docs similar to this.
{ '_id': ObjectID(62f8199dc1e8c0f11820cb91)
'name': 'foo',
'score': 4500,
'searchable': [
{
'title': 'blah',
'date': 'some_date',
'search_text': "Blah blah blah ...."
},
{
'title': 'bleep',
'date': 'some_date',
'search_text': "Lorem Lorem Lorem ...."
},
{
'title': 'bloop',
'date': 'some_date',
'search_text': "Ipsum Ipsum Ipsum ...."
}]
},
I have been trying to search for a specific string in any of the 'searchable' array objects 'search_text' fields, but filtered by 'score' (min and max). I need to return which object in 'searchable'that the string was found in, and in which document that object belongs to...
I assumed it was a job for aggregation, and $unwind but have not managed to produce a workable result. Help greatly appreciated.
using the example document above, if I searched for
"Lorem, with 'scores' between 3000 and 5000"
I want to get back that it appears in documentID: '62f8199dc1e8c0f11820cb91', 'searchable': {1}'
if more than one 'searchable' in the document matches, then return them as well, like: documentID: '62f8199dc1e8c0f11820cb91', 'searchable': {1, 2, 5,}
$objectToArrayand then search wth$filterfor example, but i dont think its worth it, yu need schema change.$filterarray operator - use the$regexMatchto find if any of the array element'ssearch_textfield has the text you are looking for. The resulting array after applying the$filterwill give you the number of matchingsearcheablearray sub-documents.