MongoDB and Node.js notes
To convert ObjectId to string use ObjectId.toString()
and to convert an string ID to ObjectId use ObjectId.createFromHexString(myStringID)
To convert ObjectId to string use ObjectId.toString()
and to convert an string ID to ObjectId use ObjectId.createFromHexString(myStringID)
const fs = require('fs');
fs.readdirSync('./path/to/directory', {
recursive: true,
withFileTypes: true
}).filter(item => !item.isDirectory()).map(item => {
const filePath = item.parentPath + '/' + item.name;
try {
const data = fs.readFileSync(filePath, 'utf8');
if (data.indexOf('string to be replaced') >= 0) {
const result = data.replace(/string to be replaced/g, replacement);
fs.writeFileSync(filePath, result);
}
} catch (err) {
console.error(err);
}
});
Serving a static json file from an API endpoint using a serverless Node.js function...