Opens the file from the database and initialize this object. Also creates a
new one if file does not exist.

open(callback)

Arguments:

callback (function) – this will be called after executing this method. The first parameter will contain an {Error} object and the second parameter will be null if an error occured. Otherwise, the first parameter will be null and the second will contain the reference to this object.

Returns:

null

Examples

A simple example showing how to save a file with a filename allowing for multiple files with the same name

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Create a file and open itvargridStore=newGridStore(db,newObjectID(),"test_gs_getc_file","w");gridStore.open(function(err,gridStore){// Write some content to the filegridStore.write(newBuffer("hello, world!","utf8"),function(err,gridStore){// Flush the file to GridFSgridStore.close(function(err,fileData){assert.equal(null,err);// Create another file with same name and and save content to itgridStore=newGridStore(db,newObjectID(),"test_gs_getc_file","w");gridStore.open(function(err,gridStore){// Write some content to the filegridStore.write(newBuffer("hello, world!","utf8"),function(err,gridStore){// Flush the file to GridFSgridStore.close(function(err,fileData){assert.equal(null,err);// Open the file in read mode using the filenamevargridStore2=newGridStore(db,"test_gs_getc_file","r");gridStore2.open(function(err,gridStore){// Read first character and verifygridStore.getc(function(err,chr){assert.equal('h',chr);// Open the file using an object idgridStore2=newGridStore(db,fileData._id,"r");gridStore2.open(function(err,gridStore){// Read first character and verifygridStore.getc(function(err,chr){assert.equal('h',chr);db.close();})});});});});});});});});});});

A simple example showing opening a file using a filename, writing to it and saving it.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Create a new instance of the gridstorevargridStore=newGridStore(db,'ourexamplefiletowrite.txt','w');// Open the filegridStore.open(function(err,gridStore){// Write some data to the filegridStore.write('bar',function(err,gridStore){assert.equal(null,err);// Close (Flushes the data to MongoDB)gridStore.close(function(err,result){assert.equal(null,err);// Verify that the file existsGridStore.exist(db,'ourexamplefiletowrite.txt',function(err,result){assert.equal(null,err);assert.equal(true,result);db.close();});});});});});

A simple example showing opening a file using an ObjectID, writing to it and saving it.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Create a new instance of the gridstorevargridStore=newGridStore(db,fileId,'w');// Open the filegridStore.open(function(err,gridStore){// Write some data to the filegridStore.write('bar',function(err,gridStore){assert.equal(null,err);// Close (Flushes the data to MongoDB)gridStore.close(function(err,result){assert.equal(null,err);// Verify that the file existsGridStore.exist(db,fileId,function(err,result){assert.equal(null,err);assert.equal(true,result);db.close();});});});});});

callback (function) – this will be called after this method is executed. The first parameter will be null and the the second will contain the reference to this object.

Returns:

null

Examples

A simple example showing how to write a file to Gridstore using file location path.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Read the filesize of file on disk (provide your own)varfileSize=fs.statSync('./test/tests/functional/gridstore/test_gs_weird_bug.png').size;// Read the buffered data for comparision reasonsvardata=fs.readFileSync('./test/tests/functional/gridstore/test_gs_weird_bug.png');// Open the new filegridStore.open(function(err,gridStore){// Write the file to gridFSgridStore.writeFile('./test/tests/functional/gridstore/test_gs_weird_bug.png',function(err,doc){// Read back all the written content and verify the correctnessGridStore.read(db,fileId,function(err,fileData){assert.equal(data.toString('base64'),fileData.toString('base64'))assert.equal(fileSize,fileData.length);db.close();});});});});

A simple example showing how to write a file to Gridstore using a file handle.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Read the filesize of file on disk (provide your own)varfileSize=fs.statSync('./test/tests/functional/gridstore/test_gs_weird_bug.png').size;// Read the buffered data for comparision reasonsvardata=fs.readFileSync('./test/tests/functional/gridstore/test_gs_weird_bug.png');// Open a file handle for reading the filevarfd=fs.openSync('./test/tests/functional/gridstore/test_gs_weird_bug.png','r',0666);// Open the new filegridStore.open(function(err,gridStore){// Write the file to gridFS using the file handlegridStore.writeFile(fd,function(err,doc){// Read back all the written content and verify the correctnessGridStore.read(db,fileId,function(err,fileData){assert.equal(data.toString('base64'),fileData.toString('base64'));assert.equal(fileSize,fileData.length);db.close();});});});});

Saves this file to the database. This will overwrite the old entry if it
already exists. This will work properly only if mode was initialized to
“w” or “w+”.

close(callback)

Arguments:

callback (function) – this will be called after executing this method. Passes an {Error} object to the first parameter and null to the second if an error occured. Otherwise, passes null to the first and a reference to this object to the second.

Returns:

null

Examples

A simple example showing how to use the write command with strings and Buffers.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Write a text stringgridStore.write('Hello world',function(err,gridStore){// Close thegridStore.close(function(err,result){assert.equal(err,null);db.close();});});});});

callback (function) – this will be called after executing this method. An exception object will be passed to the first parameter when an error occured or null otherwise. A new {Collection} object will be passed to the second parameter if no error occured.

Returns:

null

Examples

A simple example showing how to access the chunks collection object.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Access the Chunk collectiongridStore.chunkCollection(function(err,collection){assert.equal(err,null);db.close();});});});

callback (function) – this will be called after this method executes. Passes null to the first and true to the second argument.

Returns:

null

Examples

A simple example showing how to use the instance level unlink command to delete a gridstore item.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Write a text stringgridStore.write('Hello world',function(err,gridStore){// Close thegridStore.close(function(err,result){assert.equal(err,null);// Open the file again and unlin itnewGridStore(db,fileId,'r').open(function(err,gridStore){// Unlink the filegridStore.unlink(function(err,result){assert.equal(null,err);// Verify that the file no longer existsGridStore.exist(db,fileId,function(err,result){assert.equal(null,err);assert.equal(false,result);db.close();});});});});});});});

callback (function) – this will be called after executing this method. An exception object will be passed to the first parameter when an error occured or null otherwise. A new {Collection} object will be passed to the second parameter if no error occured.

Returns:

null

Examples

A simple example showing how to access the files collection object.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Access the Chunk collectiongridStore.collection(function(err,collection){assert.equal(err,null);db.close();});});});

[separator] (string) – the character to be recognized as the newline separator.

callback (function) – This will be called after this method is executed. The first parameter will be null and the second parameter will contain an array of strings representing the entire data, each element representing a line including the separator character.

Returns:

null

Examples

A simple example showing reading back using readlines to split the text into lines by the seperator provided.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Write one line to gridStoregridStore.puts("line one",function(err,gridStore){// Write second line to gridStoregridStore.puts("line two",function(err,gridStore){// Write third line to gridStoregridStore.puts("line three",function(err,gridStore){// Flush file to diskgridStore.close(function(err,result){// Open file for readinggridStore=newGridStore(db,fileId,'r');gridStore.open(function(err,gridStore){// Read all the lines and verify correctnessgridStore.readlines(function(err,lines){assert.deepEqual(["line one\n","line two\n","line three\n"],lines);db.close();});});});});});});});});

Deletes all the chunks of this file in the database if mode was set to “w” or
“w+” and resets the read/write head to the initial position.

rewind(callback)

Arguments:

callback (function) – this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.

Returns:

null

Examples

A simple example showing how to rewind and overwrite the file.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Create a new filevargridStore=newGridStore(db,fileId,"w");// Open the filegridStore.open(function(err,gridStore){// Write to the filegridStore.write("hello, world!",function(err,gridStore){// Flush the file to diskgridStore.close(function(err,result){// Reopen the filegridStore=newGridStore(db,fileId,"w");gridStore.open(function(err,gridStore){// Write some more text to the filegridStore.write('some text is inserted here',function(err,gridStore){// Let's rewind to truncate the filegridStore.rewind(function(err,gridStore){// Write something from the startgridStore.write('abc',function(err,gridStore){// Flush the data to mongodbgridStore.close(function(err,result){// Verify that the new data was writtenGridStore.read(db,fileId,function(err,data){assert.equal("abc",data);db.close();});});});});});});});});});});

Retrieves the contents of this file and advances the read/write head. Works with Buffers only.

There are 3 signatures for this method:

(callback)
(length, callback)
(length, buffer, callback)

read([length][, buffer], callback)

Arguments:

[length] (number) – the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.

[buffer] (string) – a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.

callback (function) – this will be called after this method is executed. null will be passed to the first parameter and a string containing the contents of the buffer concatenated with the contents read from this file will be passed to the second.

Returns:

null

Examples

A simple example showing the usage of the read method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Read in the content of a filevardata=fs.readFileSync('./test/tests/functional/gridstore/iya_logo_final_bw.jpg');// Create a new filevargs=newGridStore(db,"test","w");// Open the filegs.open(function(err,gs){// Write the file to GridFSgs.write(data,function(err,gs){// Flush to the GridFSgs.close(function(err,gs){// Define the file we wish to readvargs2=newGridStore(db,"test","r");// Open the filegs2.open(function(err,gs){// Set the pointer of the read head to the start of the gridstored filegs2.seek(0,function(){// Read the entire filegs2.read(function(err,data2){// Compare the file content against the orgiinalassert.equal(data.toString('base64'),data2.toString('base64'));db.close();});});});});});});});

callback (function) – This gets called after this method terminates. null is passed to the first parameter and the position is passed to the second.

Returns:

null

Examples

A simple example showing the usage of the tell method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Create a new filevargridStore=newGridStore(db,"test_gs_tell","w");// Open the filegridStore.open(function(err,gridStore){// Write a string to the filegridStore.write("hello, world!",function(err,gridStore){// Flush the file to GridFSgridStore.close(function(err,result){// Open the file in read only modevargridStore2=newGridStore(db,"test_gs_tell","r");gridStore2.open(function(err,gridStore){// Read the first 5 charactersgridStore.read(5,function(err,data){assert.equal("hello",data);// Get the current position of the read headgridStore.tell(function(err,position){assert.equal(5,position);db.close();});});});});});});});

GridStore.IO_SEEK_SET, (default) set the position from the start of the file.

GridStore.IO_SEEK_CUR, set the position from the current position in the file.

GridStore.IO_SEEK_END, set the position from the end of the file.

seek([position][, seekLocation], callback)

Arguments:

[position] (number) – the position to seek to

[seekLocation] (number) – seek mode. Use one of the Seek Location modes.

callback (function) – this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.

Returns:

null

Examples

A simple example showing the usage of the seek method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Create a file and open itvargridStore=newGridStore(db,"test_gs_seek_with_buffer","w");gridStore.open(function(err,gridStore){// Write some content to the filegridStore.write(newBuffer("hello, world!","utf8"),function(err,gridStore){// Flush the file to GridFSgridStore.close(function(result){// Open the file in read modevargridStore2=newGridStore(db,"test_gs_seek_with_buffer","r");gridStore2.open(function(err,gridStore){// Seek to startgridStore.seek(0,function(err,gridStore){// Read first character and verifygridStore.getc(function(err,chr){assert.equal('h',chr);});});});// Open the file in read modevargridStore3=newGridStore(db,"test_gs_seek_with_buffer","r");gridStore3.open(function(err,gridStore){// Seek to 7 characters from the beginning off the file and verifygridStore.seek(7,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('w',chr);});});});// Open the file in read modevargridStore5=newGridStore(db,"test_gs_seek_with_buffer","r");gridStore5.open(function(err,gridStore){// Seek to -1 characters from the end off the file and verifygridStore.seek(-1,GridStore.IO_SEEK_END,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('!',chr);});});});// Open the file in read modevargridStore6=newGridStore(db,"test_gs_seek_with_buffer","r");gridStore6.open(function(err,gridStore){// Seek to -6 characters from the end off the file and verifygridStore.seek(-6,GridStore.IO_SEEK_END,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('w',chr);});});});// Open the file in read modevargridStore7=newGridStore(db,"test_gs_seek_with_buffer","r");gridStore7.open(function(err,gridStore){// Seek forward 7 characters from the current read position and verifygridStore.seek(7,GridStore.IO_SEEK_CUR,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('w',chr);// Seek forward -1 characters from the current read position and verifygridStore.seek(-1,GridStore.IO_SEEK_CUR,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('w',chr);// Seek forward -4 characters from the current read position and verifygridStore.seek(-4,GridStore.IO_SEEK_CUR,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('o',chr);// Seek forward 3 characters from the current read position and verifygridStore.seek(3,GridStore.IO_SEEK_CUR,function(err,gridStore){gridStore.getc(function(err,chr){assert.equal('o',chr);db.close();});});});});});});});});});});});});});

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Open the file in write modevargridStore=newGridStore(db,'test_gs_empty_file_eof',"w");gridStore.open(function(err,gridStore){// Flush the empty file to GridFSgridStore.close(function(err,gridStore){// Open the file in read modevargridStore2=newGridStore(db,'test_gs_empty_file_eof',"r");gridStore2.open(function(err,gridStore){// Verify that we are at the end of the fileassert.equal(true,gridStore.eof());db.close();})});});});

callback (function) – this gets called after this method is executed. Passes null to the first parameter and the character read to the second or null to the second if the read/write head is at the end of the file.

Returns:

null

Examples

A simple example showing the usage of the seek method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Create a file and open itvargridStore=newGridStore(db,"test_gs_getc_file","w");gridStore.open(function(err,gridStore){// Write some content to the filegridStore.write(newBuffer("hello, world!","utf8"),function(err,gridStore){// Flush the file to GridFSgridStore.close(function(result){// Open the file in read modevargridStore2=newGridStore(db,"test_gs_getc_file","r");gridStore2.open(function(err,gridStore){// Read first character and verifygridStore.getc(function(err,chr){assert.equal('h',chr);db.close();});});});});});});

Writes a string to the file with a newline character appended at the end if
the given string does not have one.

puts(string, callback)

Arguments:

string (string) – the string to write.

callback (function) – this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.

Returns:

null

Examples

A simple example showing the usage of the puts method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Open a file for writingvargridStore=newGridStore(db,"test_gs_puts_and_readlines","w");gridStore.open(function(err,gridStore){// Write a line to the file using the puts methodgridStore.puts("line one",function(err,gridStore){// Flush the file to GridFSgridStore.close(function(err,result){// Read in the entire contentsGridStore.read(db,'test_gs_puts_and_readlines',function(err,data){assert.equal("line one\n",data.toString());db.close();});});});});});

autoclose (boolean) – if true current GridStore will be closed when EOF and ‘close’ event will be fired

Returns:

null

Examples

A simple example showing the usage of the stream method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Open a file for readingvargridStoreR=newGridStore(db,"test_gs_read_stream","r");// Open a file for writingvargridStoreW=newGridStore(db,"test_gs_read_stream","w");// Read in the data of a filevardata=fs.readFileSync("./test/tests/functional/gridstore/test_gs_weird_bug.png");varreadLen=0;vargotEnd=0;// Open the file we are writting togridStoreW.open(function(err,gs){// Write the file contentgs.write(data,function(err,gs){// Flush the file to GridFSgs.close(function(err,result){// Open the read filegridStoreR.open(function(err,gs){// Create a stream to the filevarstream=gs.stream(true);// Register eventsstream.on("data",function(chunk){// Record the length of the filereadLen+=chunk.length;});stream.on("end",function(){// Record the end was called++gotEnd;});stream.on("close",function(){// Verify the correctness of the read dataassert.equal(data.length,readLen);assert.equal(1,gotEnd);db.close();});});});});});});

A simple example showing how to pipe a file stream through from gridfs to a file

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Open a file for writingvargridStoreWrite=newGridStore(db,"test_gs_read_stream_pipe","w",{chunkSize:1024});gridStoreWrite.writeFile("./test/tests/functional/gridstore/test_gs_weird_bug.png",function(err,result){// Ensure we correctly returning a Gridstore objectassert.ok(typeofresult.close=='function');// Open the gridStore for reading and pipe to a filevargridStore=newGridStore(db,"test_gs_read_stream_pipe","r");gridStore.open(function(err,gridStore){// Create a file write streamvarfileStream=fs.createWriteStream("./test_gs_weird_bug_streamed.tmp");// Grab the read streamvarstream=gridStore.stream(true);// When the stream is finished close the databasefileStream.on("close",function(err){// Read the original contentvaroriginalData=fs.readFileSync("./test/tests/functional/gridstore/test_gs_weird_bug.png");// Ensure we are doing writing before attempting to open the filefs.readFile("./test_gs_weird_bug_streamed.tmp",function(err,streamedData){// Compare the datafor(vari=0;i<originalData.length;i++){assert.equal(originalData[i],streamedData[i])}// Close the databasedb.close();});})// Pipe out the datastream.pipe(fileStream);})})});

[rootCollection] (string) – the root collection that holds the files and chunks collection. Defaults to {GridStore.DEFAULT_ROOT_COLLECTION}.

callback (function) – this will be called after this method executes. Passes null to the first and passes true to the second if the file exists and false otherwise.

Returns:

null

Examples

A simple example showing the usage of the Gridstore.exist method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Open a file for writingvargridStore=newGridStore(db,null,"w");gridStore.open(function(err,gridStore){assert.equal(null,err);// Writing some content to the filegridStore.write("hello world!",function(err,gridStore){assert.equal(null,err);// Flush the file to GridFSgridStore.close(function(err,result){assert.equal(null,err);// Check if the file exists using the id returned from the close functionGridStore.exist(db,result._id,function(err,result){assert.equal(null,err);assert.equal(true,result);})// Show that the file does not exist for a random ObjectIDGridStore.exist(db,newObjectID(),function(err,result){assert.equal(null,err);assert.equal(false,result);});// Show that the file does not exist for a different file rootGridStore.exist(db,result._id,'another_root',function(err,result){assert.equal(null,err);assert.equal(false,result);db.close();});});});});});

[rootCollection] (string) – the root collection that holds the files and chunks collection. Defaults to {GridStore.DEFAULT_ROOT_COLLECTION}.

callback (function) – this will be called after this method executes. Passes null to the first and passes an array of strings containing the names of the files.

Returns:

null

Examples

A simple example showing the usage of the eof method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file idvarfileId=newObjectID();// Open a file for writingvargridStore=newGridStore(db,fileId,"foobar2","w");gridStore.open(function(err,gridStore){// Write some content to the filegridStore.write("hello world!",function(err,gridStore){// Flush to GridFSgridStore.close(function(err,result){// List the existing filesGridStore.list(db,function(err,items){varfound=false;items.forEach(function(filename){if(filename=='foobar2')found=true;});assert.ok(items.length>=1);assert.ok(found);});// List the existing files but return only the file idsGridStore.list(db,{id:true},function(err,items){varfound=false;items.forEach(function(id){assert.ok(typeofid=='object');});assert.ok(items.length>=1);});// List the existing files in a specific root collectionGridStore.list(db,'fs',function(err,items){varfound=false;items.forEach(function(filename){if(filename=='foobar2')found=true;});assert.ok(items.length>=1);assert.ok(found);});// List the existing files in a different root collection where the file is not locatedGridStore.list(db,'my_fs',function(err,items){varfound=false;items.forEach(function(filename){if(filename=='foobar2')found=true;});assert.ok(items.length>=0);assert.ok(!found);// Specify seperate idvarfileId2=newObjectID();// Write another file to GridFSvargridStore2=newGridStore(db,fileId2,"foobar3","w");gridStore2.open(function(err,gridStore){// Write the contentgridStore2.write('my file',function(err,gridStore){// Flush to GridFSgridStore.close(function(err,result){// List all the available files and verify that our files are thereGridStore.list(db,function(err,items){varfound=false;varfound2=false;items.forEach(function(filename){if(filename=='foobar2')found=true;if(filename=='foobar3')found2=true;});assert.ok(items.length>=2);assert.ok(found);assert.ok(found2);db.close();});});});});});});});});});

[offset] (number) – the offset from the head of the file of which to start reading from.

[options] (object) – the options for the file.

callback (function) – this will be called after this method executes. A string with an error message will be passed to the first parameter when the length and offset combination exceeds the length of the file while an Error object will be passed if other forms of error occured, otherwise, a string is passed. The second parameter will contain the data read if successful or null if an error occured.

Returns:

null

Examples

A simple example showing the usage of the read method.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Create a new filevargridStore=newGridStore(db,null,"w");// Read in the content from a file, replace with your ownvardata=fs.readFileSync("./test/tests/functional/gridstore/test_gs_weird_bug.png");// Open the filegridStore.open(function(err,gridStore){// Write the binary file data to GridFSgridStore.write(data,function(err,gridStore){// Flush the remaining data to GridFSgridStore.close(function(err,result){// Read in the whole file and check that it's the same contentGridStore.read(db,result._id,function(err,fileData){assert.equal(data.length,fileData.length);db.close();});});});});});

[separator] (string) – the character to be recognized as the newline separator.

[options] (object) – file options.

callback (function) – this will be called after this method is executed. The first parameter will be null and the second parameter will contain an array of strings representing the entire data, each element representing a line including the separator character.

Returns:

null

Examples

A simple example showing reading back using readlines to split the text into lines by the seperator provided.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Write one line to gridStoregridStore.puts("line one",function(err,gridStore){// Write second line to gridStoregridStore.puts("line two",function(err,gridStore){// Write third line to gridStoregridStore.puts("line three",function(err,gridStore){// Flush file to diskgridStore.close(function(err,result){// Read back all the linesGridStore.readlines(db,fileId,function(err,lines){assert.deepEqual(["line one\n","line two\n","line three\n"],lines);db.close();});});});});});});});

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Open a new file for writingvargridStore=newGridStore(db,"test_gs_unlink","w");gridStore.open(function(err,gridStore){// Write some contentgridStore.write("hello, world!",function(err,gridStore){// Flush file to GridFSgridStore.close(function(err,result){// Verify the existance of the fs.files documentdb.collection('fs.files',function(err,collection){collection.count(function(err,count){assert.equal(1,count);})});// Verify the existance of the fs.chunks chunk documentdb.collection('fs.chunks',function(err,collection){collection.count(function(err,count){assert.equal(1,count);// Unlink the file (removing it)GridStore.unlink(db,'test_gs_unlink',function(err,gridStore){// Verify that fs.files document is gonedb.collection('fs.files',function(err,collection){collection.count(function(err,count){assert.equal(0,count);})});// Verify that fs.chunks chunk documents are gonedb.collection('fs.chunks',function(err,collection){collection.count(function(err,count){assert.equal(0,count);db.close();})});});})});});});});});

Writes some data. This method will work properly only if initialized with mode “w” or “w+”.

write(data[, close], callback)

Arguments:

data (string) – the data to write.

[close] (boolean) – closes this file after writing if set to true.

callback (function) – this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.

Returns:

null

Examples

A simple example showing how to use the write command with strings and Buffers.

varDb=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');vardb=newDb('test',newServer('localhost',27017));// Establish connection to dbdb.open(function(err,db){// Our file IDvarfileId=newObjectID();// Open a new filevargridStore=newGridStore(db,fileId,'w');// Open the new filegridStore.open(function(err,gridStore){// Write a text stringgridStore.write('Hello world',function(err,gridStore){// Write a buffergridStore.write(newBuffer('Buffer Hello world'),function(err,gridStore){// Close thegridStore.close(function(err,result){// Read back all the written content and verify the correctnessGridStore.read(db,fileId,function(err,fileData){assert.equal('Hello worldBuffer Hello world',fileData.toString());db.close();});});});});});});