MongoDB Learning four----simple use of text indexing

Source: Internet
Author: User
Tags mongodb

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.