MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

बाइनरी डेटा को बेस 64 में बदलने पर अटक गया (ग्रिडफ्स-स्ट्रीम)

मैं ग्रिडफ्स से छवि को पढ़ने का समाधान भी ढूंढ रहा हूं और मैं ग्रिड-एफएस स्ट्रेम का उपयोग कर रहा हूं

मुझे यही समाधान मिला, आशा है कि यह आपके लिए उपयोगी होगा।

// ग्रिडफ़्स सेट करें

import mongoose from 'mongoose';
import Grid from 'gridfs-stream';

const db = mongoose.connection.db;
const mongoDriver = mongoose.mongo;
const gfs = new Grid(db, mongoDriver);

// छवि को मोंगो में लिखें

const writeStream = gfs.createWriteStream({
  filename: 'test.png',
  content_type: 'image/png',
});
fs.createReadStream(filePath).pipe(writeStream);

writeStream.on('close', (gfsFile) => {
  // remove the original file
  fs.unlink('test.png');
  // this is the information, and _id is the id 
  console.log(gfsFile);
});

// मोंगो को छवि पढ़ें

const readstream = gfs.createReadStream({
  _id: id,
});

const bufs = [];
readstream.on('data', function (chunk) {
  bufs.push(chunk);
});
readstream.on('end', function () {
  const fbuf = Buffer.concat(bufs);
  const base64 = fbuf.toString('base64');
  console.log(base64);
});


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. MongoDB API के साथ CosmosDB से कनेक्ट करते समय समयबाह्य त्रुटि

  2. बड़ी संख्या में दस्तावेज़ों के लिए साझाकरण कुंजी (MongoDB)

  3. आप नोड के भीतर MongoDB संग्रह में दस्तावेज़ों की मात्रा की गणना कैसे करते हैं?

  4. मोंगो डीबी किसी भी मूल्य के साथ क्षेत्र के नाम से खोजें

  5. खुलने का समय MongoDB के लिए स्कीमा