db-migrate / mongodb

mongodb driver for db-migrate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for Mongo 2dsphere indexes

mefernandez opened this issue · comments

From this example:
https://docs.mongodb.com/manual/tutorial/geospatial-tutorial/#prerequisites

It would be nice if db-migrate could support this:
db.restaurants.createIndex({ location: "2dsphere" })

This currently can't be done with addIndex NoSQL API.

Workaround: loading native mongodb driver:

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://mongo:27017/test';

exports.up = function(db, callback) {
  MongoClient.connect(url, function(err, dbnative) {
        assert.equal(null, err);
        assert.notEqual(null, dbnative);
        dbnative.collection('ubicaciones').createIndex({ location: "2dsphere" })
        dbnative.close();
        callback();
      });
  return null;
}

<bountysource-plugin>

---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/46557329-add-support-for-mongo-2dsphere-indexes?utm_campaign=plugin&utm_content=tracker%2F12293389&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F12293389&utm_medium=issues&utm_source=github).
</bountysource-plugin>

Another workaround

exports.up = async function (db) {
  const options = { run_on: new Date() };
  const mongoDbNative = await db._run('getDbInstance', null, options);
  await mongoDbNative
    .collection(COLLECTION_NAME)
    .createIndex(
      { location: '2dsphere' },
      { name: INDEX_NAME, background: true }
    );
  await mongoDbNative.close();
};