In the study of the index of MongoDB is to find a strange problem, set the text index to a string field, but did not use the index when querying. Like what:
Db.tomcat_access_logs.ensureIndex ({url: ' text '});
Db.tomcat_access.logs.find ({url: ' 1 '}). explain ();
Db.tomcat_access_logs.find ({url:/1/}). Explain ();
{
"Cursor": "Basiccursor",
"Ismultikey": false,
"N": 0,
"Nscannedobjects": 100,
"nscanned": 100,
"Nscannedobjectsallplans": 100,
"Nscannedallplans": 100,
"Scanandorder": false,
"Indexonly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 0,
...
}
From the results of explain (), you can find that only basiccursor is used when querying, that is, no index is used.
It is found that the text index is used only when $text query is used:
Db.tomcat_access_logs.find ({$text: {$search: ' 1 '}}). explain ();
{
"Cursor": "Textcursor",
"N": 0,
"Nscannedobjects": 0,
"nscanned": 0,
"Nscannedobjectsallplans": 0,
"Nscannedallplans": 0,
"Scanandorder": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 0,
...
}
In this case, there is no way to query for a particular field because $text is a full-text search for the field of all text indexes. You only need to do a general index at this point:
Db.tomcat_access_logs.ensureIndex ({url:1});
Db.tomcat_access.logs.find ({url: ' 1 '}). explain ();
Db.tomcat_access.logs.find ({url:/.*1.*/g}). Explain ();
{
"Cursor": "Btreecursor url_1",
"Ismultikey": false,
"N": 0,
"Nscannedobjects": 0,
"nscanned": 100,
"Nscannedobjectsallplans": 0,
"Nscannedallplans": 100,
"Scanandorder": false,
"Indexonly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 1,
"Indexbounds": {
"url": [
[
"",
{
}
],
[
/.*1.*/,
/.*1.*/
]
]
},
...
}
Summarize
Using Db.collection.find ({url: ' 1 '}) or Db.collection.find ({URL:/.*a.*/}), the text index is not used, but the general index.
After you have established the text index, you can only search for full-text in all fields that are contained in the text index, and you cannot search for a field
General index and text index can be established at the same time to meet different query requirements
Efficiency of indexing
Can the index of MongoDB improve query efficiency? We are here to test it by an example. Compare the query speed of the same data without indexing and indexing.
First, we insert a 10W piece of data in such a way:
public void Insertbigdata ()
{
var random = new Random ();
for (int i = 1; i < 100000; i++)
{
Document doc = new document ();
doc["ID"] = i;
doc["Data" = "data" + random. Next (100000);
Mongocollection.save (DOC);
}
Console.WriteLine ("currently has" + Mongocollection.findall ()). Documents.count () + "strip Data");
}
Then, implement a method to create the index:
public void Createindexfordata ()
{
MongoCollection.Metadata.CreateIndex (new Document {{"Data", 1}}, False);
}
There are also methods of sorting:
public void Sortfordata ()
{
Mongocollection.findall (). Sort (new Document {{"Data", 1}});
}
Run the test code as follows:
static void Main (string[] args)
{
INDEXBLL INDEXBLL = new INDEXBLL ();
Indexbll.dropallindex ();
Indexbll.deleteall ();
Indexbll.insertbigdata ();
Stopwatch watch1 = new stopwatch ();
Watch1. Start ();
for (int i = 0; i < 1; i++) Indexbll.sortfordata ();
Console.WriteLine ("No Index sort Execution Time:" + watch1.) Elapsed);
Indexbll.createindexfordata ();
Stopwatch WATCH2 = new stopwatch ();
Watch2. Start ();
for (int i = 0; i < 1; i++) Indexbll.sortfordata ();
Console.WriteLine ("Have index sort execution time:" + WATCH2.) Elapsed);
}
Final execution Program View results:
Multiple tests have shown that, in the case of an index, the query is more efficient than without indexing.