I need to search by a keyword into the documents of my DB and I have found this on mongo's official site documentation. However, I have few problems:
I need to make a lookup first, and mongo wants the $text
search as the first pipeline, so If I make after that the lookup then I exclude the fields from the lookup.
I need to search only between some fixed fields and not everywhere.
For example my documents looks like this:
{
_id: ObjectId('60dcb67ebe94846b27402dc7'),
field1: "dada",
field2: "lalala",
field3: "nanana",
creator: ObjectId('632cb67ebe94846b27402dc1')
}
I Need to search in field1
, field2
and some fields from creator , let's say creator.name
, which I'll have got after doing the lookup. According to the documentation first thing first I have to create an index so let's do it on field1
. After that I have to put the $text
aggregation in a $match
pipeline, but as its first one.
Do you think it is possible to do first the lookup and after that the $text search? Also is it possible to specify the fields to search in?
To perform text search queries, you must have a text index on your collection. A collection can only have one text search index, but that index can cover multiple fields. You cannot create test index spanning multiple collections.
Also, The $match
stage that includes a $text
must be the first stage in the pipeline.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.