MongoDB database tips

Source: Internet
Author: User

1. When you call ensureindex to create an index, you must ensure that no index has been created on the corresponding key before. Otherwise, if the statement is not executed, the index will retain the old version.

Example: we can create a common index for people: DB. People. ensureindex ({"Age": 1 });
Then we can create a unique index for the age key again: DB. People. ensureindex ({"Age": 1 },{ "unique": true })

If the original data contains duplicate age keys, the creation will fail and an error will be reported, but we will not encounter this situation, and the statement will return soon.

2. Options for creating indexes: DB. collectionname. ensureindex (index dictionary, option dictionary)

A. Unique: {"unique": true}. The index value is unique, that is, any two documents are mutually exclusive.

B. Background: {"background": true}. Create an index in the background. It takes a lot of time to create an index, so it is better in the background.

To be continued ......

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.