0

I am looking for a sample example for storing videos/images in MongoDB using GridFS. I came across this official site and followed the below code snippet

var MongoClient = require('mongodb').MongoClient,
Grid = mongo.Grid;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
  if(err) return console.dir(err);
  var grid = new Grid(db, 'fs');
  var buffer = new Buffer("Hello world");
  grid.put(buffer, {metadata:{category:'text'}, content_type: 'text'}, function(err, fileInfo) {
    if(!err) {
      console.log("Finished writing file to Mongo");
    }
  });
});

When I run the code I am getting bellow error

Grid = mongo.Grid;
       ^
ReferenceError: mongo is not defined
at Object.<anonymous> (D:\Rahul\Nodejs\Fileupload\fileupload\Samples\grid-fs
\mongo.js:3:8)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3

please share any link for How to store files in MongoDB using GridFS in Nodejs.

EDIT 1

I tried a lot to get it work and finally I come to know the interface Grid is removed from Mongodb 2.1 so It won't work but there is no documentation for the replacement of Grid. Now I don't have clear idea about which one to use i.e GridStore, GridFSBucket, etc

Is this documentation misleading?

4
  • Hate to state the obvious, is mongo defined? i bet it isn't. Commented Apr 11, 2016 at 11:14
  • @BanksySan I dont have any idea what kind of reference is that, i took it as it is from site, can you please post the answer if you know that Commented Apr 11, 2016 at 11:18
  • It's telling you that the variable mongo, on line 3 is undefined. Can't be more explicit than that. Commented Apr 11, 2016 at 11:38
  • thank you @BanksySan, I just want to know where mongo variable should point, like var mongo = require('mongodb'); Commented Apr 11, 2016 at 15:18

1 Answer 1

1

I believe what you want is:

var mongo = require('mongodb');

The things off the require('mongodb'}:

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

From the documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank for the answer I tried this and got Grid is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.