To improve query efficiency, MONGODB also provides indexes (like the index of relational databases, using B-tree storage), with a single field index, combination index, multiple key index (for array query), spatial index, text index, and hash index.
Since version 2.4, MongoDB has provided a text index, let's look at how to use it.
1. Start a single mongod, note to set the value of textsearchenabled
Mongod--dbpath dbtext--port 28017--setparameter
2. Connect Mongod:mongo localhost:28017
3. Insert several records into the database:
Use test
A. Db.tb.insert ({"Name": "WZ", "Engname": "Ryan", "Age": 29})
B. Db.tb.insert ({"Name": "XW", "Engname": "Ryans", "Age": 29})
C. Db.tb.insert ({"Name": "Wxc", "Engname": "Baby", "Age": 2})
D. Db.tb.insert ({"Name": "Ryan", "Engname": "WZ", "Age": 29})
E. Db.tb.insert ({"Name": "Bu", "engname": "Bill", "Age": "Ryan"})
To build a text index on the name and Engname two fields:
Db.tb.ensureIndex ({"Name": "Text", "Engname": "Text"})
Then search "Ryan", Db.tb.runCommand ("text", {search: "Ryan"}), and query to a B D 3 record
Delete the above index, Db.dropindex ("Name_text_engname_text"),
Create a text index for all fields, Db.ensureindex ({"$**": "Text"}), re-execute the search, and query to a B D E 4 record