Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

does-index-exist

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

does-index-exist

Checks whether an index exists on a MongoDB collection

  • 0.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

does-index-exist

Checks whether an index exists on a MongoDB collection

build status

Installation

npm install does-index-exist --save

Usage

var doesIndexExist = require('does-index-exist')
  , MongoClient = require('mongodb').MongoClient
  , collection = 'collection'

MongoClient.connect('mongodb://localhost:27017/does-index-exist', function (err, db) {
  var indexExist = doesIndexExist({ connection: db, collection: collection })

  indexExist({ key: { field: 1 } }, function (err, exists) {
    console.log('Index does not exist:', exists)

    // Create index
    db.ensureIndex(collection, { field: 1 }, function () {

      indexExist({ key: { field: 1 } }, function (err, exists) {
        console.log('Index does exist:', exists)

        // Drop index
        db.dropIndex(collection, 'field_1', process.exit)
      })

    })

  })
})

var indexExist = doesIndexExist(options)

Options must include:

  • connection - a database connection returned from MongoClient.connect
  • collection - the collection name to check for the index

This returns a function which is called like so:

indexExist(cb)

  • cb is the callback for when the query is complete, called with cb(err, exists). err is any mongo errors, exists is a boolean

Credits

Dom Harrington

Keywords

FAQs

Package last updated on 11 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc