GridFS is a specification for storing and retrieving large files in MongoDB. It divides a file into smaller chunks and stores each chunk as a separate document. When working with GridFS in a Node.js environment using Mongoose.js, you can follow these steps: 1. Set Up Your Project First, set up yourRead more
GridFS is a specification for storing and retrieving large files in MongoDB. It divides a file into smaller chunks and stores each chunk as a separate document. When working with GridFS in a Node.js environment using Mongoose.js, you can follow these steps:
1. Set Up Your Project
First, set up your Node.js project if you haven’t already.
mkdir gridfs-example
cd gridfs-example
npm init -y
npm install mongoose gridfs-stream
2. Connect to MongoDB
Set up your MongoDB connection using Mongoose.
3. Set Up GridFS Stream
Use gridfs-stream
to interact with GridFS.
4. Upload a File to GridFS
Use the gridfs-stream
to upload a file.
5. Retrieve a File from GridFS
Use gridfs-stream
to read a file.
6. Delete a File from GridFS
Use gfs.remove
to delete a file.
Full Example
Here is a complete example incorporating all the steps:
const mongoose = require(‘mongoose’);
const Grid = require(‘gridfs-stream’);
const fs = require(‘fs’);
const path = require(‘path’);
// Connect to MongoDB
mongoose.connect(‘mongodb://localhost:27017/gridfs-example’, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const conn = mongoose.connection;
Grid.mongo = mongoose.mongo;
let gfs;
conn.once(‘open’, () => {
console.log(‘MongoDB connected’);
gfs = Grid(conn.db);
gfs.collection(‘uploads’);
// Upload a file
const filePath = ‘/path/to/your/file.txt’;
const writeStream = gfs.createWriteStream({
filename: path.basename(filePath),
});
fs.createReadStream(filePath).pipe(writeStream);
writeStream.on(‘close’, (file) => {
console.log(`File ${file.filename} written to DB`);
// Retrieve the file
const filename = file.filename;
const destination = `/path/to/destination/${filename}`;
const readStream = gfs.createReadStream({ filename });
const writeStream = fs.createWriteStream(destination);
readStream.pipe(writeStream);
writeStream.on(‘close’, () => {
console.log(`File ${filename} has been written to ${destination}`);
// Delete the file
gfs.remove({ filename }, (err) => {
if (err) {
console.error(‘Error deleting file:’, err);
} else {
console.log(`File ${filename} deleted from DB`);
}
});
});
});
}
Roles of Introns in DNA: 1. Introns, previously thought of as useless DNA, have significant functions in controlling gene expression. They enable alternative splicing, enabling one gene to create various proteins, thus enhancing protein variety without increasing genome size. 2. In addition, intronsRead more
Roles of Introns in DNA:
1. Introns, previously thought of as useless DNA, have significant functions in controlling gene expression. They enable alternative splicing, enabling one gene to create various proteins, thus enhancing protein variety without increasing genome size.
2. In addition, introns play a role in regulating genes by containing elements that impact how genes are expressed. They have the ability to impact mRNA stability and transportation, which guarantees proper and timely synthesis of proteins.
3. Introns play a role in evolution by offering opportunities for genetic recombination and mutation, potentially resulting in the development of novel gene functions.
4. Their existence in DNA highlights the intricate nature of genomic regulation and the advanced methods of controlling gene expression.
See less