If there is a common index, queries with fixed prefix can use the index. But I don’t know what startswith method you are talking about. If it is in the driver, it depends on the driver and what its documentation says. The prefix I am referring to is the matching of regular expressions starting with ^, such as:
db.collection.find({name: /^张/})
If you look at the execution plan, you can see IX_SCAN:
db.collection.find({name: /^张/}).explain(true)
Full-text index is another thing. Simply put, you don’t need to consider whether the prefix is fixed, but the basic unit of full-text index is 词, not a word. So the following sentence:
First Division is a good school
When searching, you can directly find the word you want, such as:
db.collection.find({ $text: { $search: "好学校" } })
But if you directly search for "school", you may not be able to find it:
Of course, this is related to how different word segmentation engines are divided. The example is just to illustrate the limitations of full-text indexing. In addition, articles, quantifiers and other words that have no actual meaning will also be filtered out, such as "人":
In addition, you may also notice that the above query does not mention name this field at all, because there can only be one full-text index on a collection.
If there is a common index, queries with fixed prefix can use the index. But I don’t know what startswith method you are talking about. If it is in the driver, it depends on the driver and what its documentation says. The prefix I am referring to is the matching of regular expressions starting with
^
, such as:If you look at the execution plan, you can see
IX_SCAN
:Full-text index is another thing. Simply put, you don’t need to consider whether the prefix is fixed, but the basic unit of full-text index is
词
, not a word. So the following sentence:When searching, you can directly find the word you want, such as:
But if you directly search for "school", you may not be able to find it:
Of course, this is related to how different word segmentation engines are divided. The example is just to illustrate the limitations of full-text indexing.
In addition, articles, quantifiers and other words that have no actual meaning will also be filtered out, such as "人":
In addition, you may also notice that the above query does not mention
name
this field at all, because there can only be one full-text index on a collection.