Search code examples
javascriptmongodbgridfs-stream

How to use monk initiated mongoDB instance in gridfs-stream?


I'm using gridfs-stream. In the docs there is an example for a mongodb native DB:

// create or use an existing mongodb-native db instance.
var db = new mongo.Db('yourDatabaseName', new mongo.Server("127.0.0.1", 27017))
// make sure the db instance is open before passing into `Grid`
db.open(function (err) {
  if (err) return handleError(err);
  var gfs = Grid(db, mongo);
})

...and I'm using monk, which is also based on mongodb native driver.

Unfortunately I am not very familiar with implementation of mongoDB, so I'm not quite sure, if I can also use a monk connected db with gridfs-stream:

var db = monk('mongodb://localhost:27017/yourDatabaseName')
var gfs = Grid(db, mongo) // <-- what is mongo in this context?

...with this attempt I do not know from where to get mongo


Solution

  • mongo in this context is just mongodb, something like this:

    const mongo = require('mongodb'),
          monk = require('monk'),
          monkMgr = monk('mongodb://localhost:27017/yourDatabaseName');
    
    monkMgr.on("open", db => {
        const gfs = Grid(db, mongo);
    });
    

    It seems Grid uses the long deprecated new mongo.Db( instead of MongoClient.connect whilst monk relies on more recent version of the driver. I would expect some incompatibility. Just be aware of risks and invest extra time in testing.