Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating ensure Index in Mogodb

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 734
    Comment on it

    Creating Indexes in Mogodb- ensure Index and get indexes then verify

    Hello readers!

    Many time we get stuck with the query How to Creating Indexes in Mogodb, ensure Index and get indexes then verify?
    Here below is the simple answer for this query-
    Indexes are used to support efficient and faster performance of queries. Indexes are special data structure that holds a small part of data set in an cinch to track form.
    MongoDb supports indexes by using ensureIndex() method.


    Basic syntax of ensureIndex() method is as follows:

    db.COLLECTION_NAME.ensureIndex({KEY:1})
    
    Note: KEY:1 = ASC sorting and KEY:-1 = DESC sorting.
    
    Example:
    db.employees.ensureindex({title:1,salary:-1})
    

    Above example will output all employees data where employees title will be sorted in ascending order and employees salary will be sorted in descending order.

    ensureIndex() method also accepts list of options (which are optional), whose list is given below:

    Parameter

    Type

    Description

    Background

    Boolean

    Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false.

    unique

    Boolean

    Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index.

    dropDups

    Boolean

    Creates a unique index on a field that may have duplicates. MongoDB indexes only the first occurrence of a key and removes all documents from the collection that contain subsequent occurrences of that key. Specify true to create unique index. The default value is false.

    Name

    String

    The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.

    sparse

    Boolean

    If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false.

    expireAfterSeconds

    Integer

    Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection.

    V

    Index version

    The index version number. The default index version depends on the version of mongod running when creating the index.

    weights

    document

    The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score.

    default_language

    String

    For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is english.

    language_override

    String

    For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language.

    View all indexes on the employees collection:

    db.employees.getIndexes()
    

    Above query will output of all indexes available in employees table. eg.

    {
        "0" : {
            "v" : 1,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "db.employees"
        },
        "1" : {
            "v" : 1,
            "unique" : true,
            "key" : {
                "email" : 1
            },
            "name" : "email_1",
            "ns" : "db.employees",
            "background" : true,
            "safe" : null
        },
        "2" : {
            "v" : 1,
            "unique" : true,
            "key" : {
                "email" : 1,
                "deleted" : 1
            },
            "name" : "email_1_deleted_1",
            "ns" : "db.employees",
            "background" : true,
            "safe" : null
        }
    }
    

    Drop an index on the employees collection:

    Below syntax is used for delete or remove existing specified index from the employees collection.

    db.collection.dropIndex(index)
    

    Here index either by the index name or by the index specification document. eg.

    db.employees.dropIndex( "email_1" ) - will remove index where name is email_1
    
    db.employees.dropIndex( { "email" : -1 } ) - will remove index where key = email: -1
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: