Der er et par løsninger. Du kan bruge writeBuffer, writeFile eller den nye simple grid-klasse. Under er dit eksempel justeret for at bruge en bufferforekomst.
// You can use an object id as well as filename now
var gs = new mongodb.GridStore(this.db, filename, "w", {
"chunk_size": 1024*4,
metadata: {
hashpath:gridfs_name,
hash:hash,
name: name
}
});
gs.open(function(err,store) {
// Write data and automatically close on finished write
gs.writeBuffer(data, true, function(err,chunk) {
// Each file has an md5 in the file structure
cb(err,hash,chunk);
});
});
Generelt er det bedste sted at starte de test, der dækker en bred brugsprofil for gridfs klasserne. Se på.
https://github.com/christkv/node-mongodb-native/tree/master/test/gridstore