Skip to content

Instantly share code, notes, and snippets.

@pfleidi
Created December 19, 2010 22:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfleidi/747778 to your computer and use it in GitHub Desktop.
Save pfleidi/747778 to your computer and use it in GitHub Desktop.
Example using the mongodb-native module and step to write to mongodb gridfs
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server,
GridStore = require('mongodb').GridStore,
Step = require('step'),
Sys = require('sys'),
Fs = require('fs');
var database;
Step(
function openDb() {
var db1 = new Db('node-mongo-examples', new Server('localhost', Connection.DEFAULT_PORT, {}), {native_parser:true});
db1.open(this);
},
function opendStore(err, db) {
if (err) throw err;
database = db;
var gridStore = new GridStore(db, "somepicture", "w", {
'content_type': 'image/jpeg',
'metadata': { 'tes0r': 2342 },
'root': 'images'
});
gridStore.open(this.parallel());
Fs.readFile('./201713246bbfb9efde162aee3b14ec0b3d4101d4.jpg', this.parallel());
},
function writeToGridStore(err, gridStore, image) {
if (err) throw err;
gridStore.write(image, this);
},
function closeStore(err, gridStore) {
if (err) throw err;
gridStore.close(this);
},
function reOpenStore(err, result) {
var gridStore = new GridStore(database, "somepicture", "w", {
'root': 'images'
});
gridStore.open(this);
},
function readFromStore(err, gridStore) {
if (err) throw err;
Sys.puts("contentType: " + gridStore.contentType);
Sys.puts("uploadDate: " + gridStore.uploadDate);
Sys.puts("chunkSize: " + gridStore.chunkSize);
Sys.puts("metadata: " + Sys.inspect(gridStore.metadata));
gridStore.close(this);
},
function finish(err, result) {
if (err) throw err;
console.dir(result);
database.close();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment