A class representation of a simple Grid interface.
- class Grid()¶
Arguments:
- db (db) – A database instance to interact with.
- [fsName] (string) – optional different root collection for GridFS.
Returns: grid
Puts binary data to the grid
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple example showing the usage of the put method.
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'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a new grid instance var grid = new Grid(db, 'fs'); // Some data to write var originalData = new Buffer('Hello world'); // Write data to grid grid.put(originalData, {}, function(err, result) { // Fetch the content grid.get(result._id, function(err, data) { assert.deepEqual(originalData.toString('base64'), data.toString('base64')); db.close(); }); }); });A simple example showing the usage of the put method.
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'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a new grid instance var grid = new Grid(db, 'fs'); // Some data to write var originalData = new Buffer('Hello world'); // Write data to grid var id = 123; grid.put(originalData, {_id: id}, function(err, result) { // Fetch the content grid.get(id, function(err, data) { assert.deepEqual(originalData.toString('base64'), data.toString('base64')); db.close(); }); }); });A simple example showing the usage of the put method.
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'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a new grid instance var grid = new Grid(db, 'fs'); // Some data to write var originalData = new Buffer('Hello world'); // Write data to grid var id = 'test'; grid.put(originalData, {_id: id}, function(err, result) { assert.equal(result._id, id); // Fetch the content grid.get(id, function(err, data) { assert.deepEqual(originalData.toString('base64'), data.toString('base64')); db.close(); }); }); });
Get binary data to the grid
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple example showing the usage of the get method.
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'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a new grid instance var grid = new Grid(db, 'fs'); // Some data to write var originalData = new Buffer('Hello world'); // Write data to grid grid.put(originalData, {}, function(err, result) { // Fetch the content grid.get(result._id, function(err, data) { assert.deepEqual(originalData.toString('base64'), data.toString('base64')); // Should fail due to illegal objectID grid.get('not an id', function(err, result) { assert.ok(err != null); db.close(); }); }); }); });
Delete file from grid
Arguments: |
|
---|---|
Returns: | null |
Examples
A simple example showing the usage of the delete method.
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'); var db = new Db('test', new Server('localhost', 27017)); // Establish connection to db db.open(function(err, db) { // Create a new grid instance var grid = new Grid(db, 'fs'); // Some data to write var originalData = new Buffer('Hello world'); // Write data to grid grid.put(originalData, {}, function(err, result) { // Delete file grid.delete(result._id, function(err, result2) { assert.equal(null, err); assert.equal(true, result2); // Fetch the content, showing that the file is gone grid.get(result._id, function(err, data) { assert.ok(err != null); assert.equal(null, data); db.close(); }); }); }); });