Created
December 19, 2010 22:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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