sql >> Base de Datos >  >> NoSQL >> MongoDB

Problema con MongoDB GridFS al guardar archivos con Node.JS

Hay un par de soluciones. Puede usar writeBuffer, writeFile o la nueva clase de cuadrícula simple. Debajo está su ejemplo ajustado por el hecho de usar una instancia de búfer.

// 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);
  });
});

En general, el mejor lugar para comenzar son las pruebas que cubren un amplio perfil de uso para las clases gridfs. Mira.

https://github.com/christkv/node-mongodb-native/tree/master/test/gridstore