The index type of MongoDB learning note 25 MongoDB

Source: Internet
Author: User

Create an index You can specify options, and indexes that are built with different options behave differently.

First, unique index

A unique index ensures that each document has a unique value for the specified key. For example, to determine that the username key in a document is not duplicated, create a unique index:

> Db.users.findOne () {"_id": ObjectId ("54ad5826245d1b7d58b53238"), "I": 2, "username": "User2", "Age": "Created": Isodate ("2015-01-07t16:00:38.634z")}
> Db.users.ensureIndex ({"username": 1},{"unique": true}) {"createdcollectionautomatically": false, "num Indexesbefore ": 1," Numindexesafter ": 2," OK ": 1}


> Db.users.insert ({"username": "User2"}) Writeresult ({"ninserted": 0, "Writeerror": {"Co De ": 11000," errmsg ":" Insertdocument:: Caused by:: 11000 E11000 duplicate key error index:blog.users. $use Rname_1 dup key: {: \ "user2\"} "}}) >

You can see that after creating a unique index, you cannot insert the same key value;

1. Unique composite Index

Create a unique composite index, the single-key value of a composite index can be duplicated, but the combined key values that make up a unique composite index can only be unique. For example:

If there is a {"username": "User2", "Age": 18} has a unique index, the following insert is legal,

>db.users.insert ({"username": "Bob"}) >db.users.insert ({"username": "Bob", "Age": All}) >db.users.insert ({" Username ":" Fred "," Age ": 23})

If you insert any of these three items again, it will cause the key to repeat the exception;

2. To repeat

Creating a unique index on a field that already has duplicate values causes an error; You can use the Dropdups option to encounter duplicate values that retain the first, removing other duplicates;

>db.users.ensureindex ({"username": 1},{"unique": true, "dropdups": true})

Use this option with caution, because there is no way to control which documents are deleted;

Second, sparse index

If there is a field that may or may not exist, the index created on this field is called a sparse index.

To create a unique sparse index:

>db.users.ensureindex ({"username": 1},{"unique": true, "sparse": true})

To create a non-unique sparse index:

>db.users.ensureindex ({"username": 1},{"sparse": true})



This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1601478

The index type of MongoDB learning note 25 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.