简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM