Categorygithub.com/kei2100/go-mongo-indexer
2.0.0-fork.kei2100.9
Repository: https://github.com/kei2100/go-mongo-indexer.git
Documentation: pkg.go.dev

# README

go-mongo-indexer

CLI utility to manage mongodb collection indexes

Blog URL Build Status Go Report Card

Installation

go install github.com/kei2100/go-mongo-indexer/v2/cmd/indexer@latest

Usage

indexer -config <index-config-file> 
        -uri <mongodb-connection-uri>
        -database <database name>
        -apply

Details of options is listed below

OptionRequired?Description
configYesPath to indexes configuration file
uriYesMongoDB connection string e.g. mongodb://127.0.0.1:27017
databaseYesDatabase name
applyNoWhether to apply the indexes on collections or not. If not given, it will show the plan that will be applied

Config Format

The configuration file is just a simple json file containing the indexes to be applied. This file is an array of objects. Where each object has details like collection name, cap size and indexes for this specific collection.

[
    {
        "collection": "order",     // name of collection
        "cap": null,               // Number of bytes 
        "index": [                 // Array of index details
            {
                "key": [
                    { "cartId": 1 }   // An ascending order index
                ]
            },
            {
                "key": [
                    { "status": -1 } // Descending order index
                ]
            },
            {
                "key":[
                    // Composite index on orderNumber and type
                    { "orderNumber": 1 },
                    { "type": 1 }
                ]
            },  
            {
                "key": [
                    { "type": -1 }, 
                    { "paymentStatus": -1 }, 
                    { "payment.paymentMethod": -1 }
                ]
            }
        ]
    },
    {
        "collection": "collection_name",
        "cap": null,
        "index": [
            {
                "key": [
                    { "orderId": 1 }
                ],
                "unique": true, // creates a `unique index`
                "sparse": true  // creates a `sparse index`
            },                       
            {
                "key": [
                    { "createdAt": -1 }
                ], 
                "expireAfterSeconds": 500 // creates a `expires index` that will delete document after given number of seconds 
            }       
        ]
    }
    ....
    ....
    ....
]

Note capping is still progress not yet supported

Examples

See list of index changes before applying

indexer -config "/path/to/xyz.json" -uri "mongodb://127.0.0.1:27017/database_name" -database "database_name"

Apply the index changes

$ indexer -config "/path/to/xyz.json" -uri "mongodb://127.0.0.1:27017/database_name"  -database "database_name" --apply

Todo

  • Write tests
  • Collection capping
  • Support for _unique and _expireAfterSeconds indexes

Contributing

Anyone is welcome to contribute, however, if you decide to get involved, please take a moment to review the guidelines:

  • Only one feature or change per pull request
  • Write meaningful commit messages
  • Follow the existing coding standards

License

MIT © Adnan Ahmed

# Packages

No description provided by the author