const MDBClient = require('mongodb').MongoClient const url = process.env.MONGO_URL if ( !process.env.MONGO_URL || !process.env.DB_NAME || !process.env.COLLECTION_NAME || !process.env.COMMAND || !process.env.ARGUMENT ) { throw new Error(`process.env.MONGO_URL ${process.env.MONGO_URL?.replace(/./ig, '*')} process.env.DB_NAME ${process.env.DB_NAME} process.env.COLLECTION_NAME ${process.env.COLLECTION_NAME} process.env.COMMAND ${process.env.COMMAND} process.env.ARGUMENT ${process.env.ARGUMENT} Данные переменные окружения должны быть заполнены`) } const mongoDBConnect = async () => { try { const MongoClient = new MDBClient(url, { useUnifiedTopology: true, }) return await MongoClient.connect() } catch (error) { console.error(error) } } console.log('try connect') mongoDBConnect().then(async (client) => { console.log('Connected to MongoDB') try { const db = await client.db(process.env.DB_NAME) const collection = await db.collection(process.env.COLLECTION_NAME) console.log('command', process.env.COMMAND) console.log('argument', process.env.ARGUMENT, process.env.ARGUMENT.replace("ISODate()", new Date().toISOString())) const answer = await collection[process.env.COMMAND](JSON.parse(process.env.ARGUMENT.replace("ISODate()", `${new Date('2024-08-27T12:14:23.160Z').toISOString()}`))) await client.close(); return answer.ops?.[0] ?? answer } catch (error) { await client.close(); } }) .then(console.log) .catch(console.error)