The index on MongoDB

Source: Internet
Author: User

1. The index is built on the number key named Nameindex and is a positive-order index ({number:-1} is a reverse index) such as: Db.list name. Ensureindex ({number:1},{name: "Nameindex"});

2. Specify the use of a positive-order index when querying hint () For example: Db.list name. Find ({age:{$lte: 56}},{name:1,_id:0}). Hint ({name:1});

3. At the time of query, which index do we use such as: Db.list name. Find ({age:{$lte:]). explain (); You can print out the index information used;

4. Indexing sometimes consumes performance then we can execute it in the background using the Db.list name. Ensureindex ({number:1},{background:true});

5. Create a unique index, meaning that the contents of each key of the index cannot have duplicates such as: Db.list name. ensureindex ({number:1},{unique:true,dropdups:true}); Dropdups:true control Delete (optional) If there are duplicates * * * But the meaning of the unique index is not large * * *;

6. Delete the specified index (name_-1 is the index name) such as: Db.runcommand ({dropindexes: "list name", Index: "Name_-1"}), complete the deletion of the index, or use the db.list name directly. Drop (); Also available;

If you want to delete all indexes such as: Db.runcommand ({dropindexes: "list name", Index: "*"});

The index on MongoDB

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.