// Use connect method to connect to the Server setTimeout(() => { client.connect(function(err) { if (err) { return console.error(err); } console.log("Connected successfully to database"); db = client.db(MONGO_DATABASE_NAME); }); }, 2000);
app.post('/collect', (req, res) => { console.log(req.body); const client = new MongoClient(process.env.MONGO_URL); client.connect((err) => { const db = client.db(); const collection = db.collection('events'); collection.insertOne({ timestamp: new Date(), ...req.body }, ((err, result) => { client.close(); res.send('ok'); })); }); });
_connect() { if (!this._connectionPromise) { this._connectionPromise = MongoClient.connect( this._databaseURI, this._mongoOptions ).then(client => { this._client = client; return client.db(client.s.options.dbName); }); } return this._connectionPromise; }
MongoClient.connect('mongodb://localhost:27017/admin', {useNewUrlParser: true}, (err, mconn) => { if(err) { console.error(err) process.exit(1) } torrentdb = mconn.db('torrent') torrentdb.collection('log').createIndex({date: 1, reqs: -1}) torrentdb.collection('log').createIndex({hash: 1}) torrentdb.collection('hash').createIndex({hash: 1}, {unique: 1}) torrentdb.collection('hash').find().sort({_id: -1}).limit(1).next((err, r) => { nextTorrentId = (r && r._id || 0) + 1 console.log('Next torrent _id is', nextTorrentId, 'tableCaption', process.env.TABLE_CAPTION) }) })
async function startServer() { const client = await MongoClient.connect('mongodb://localhost:27017/admin', {useNewUrlParser: true}) app.context.torrentdb = client.db('torrent') app.context.mdb = await mysql.createPool({ connectionLimit: 5, host: 'localhost', user: 'root', password: '', port: 9306, multipleStatements: true }) app.listen(process.env.PORT || 3000, async () => { console.log(new Date(), 'Server is istening...') if(process.send) { process.send('ready') } }) }
_connect() { if (!this._connectionPromise) { this._connectionPromise = _mongodb.MongoClient.connect(this._databaseURI, this._mongoOptions).then(client => { this._client = client; return client.db(client.s.options.dbName); }); } return this._connectionPromise; }
connect() { return new Promise((resolve, reject) => { this.mongoClient.connect((err, client) => { if (err) { return reject(err); } this.client = client; this.db = this.client.db(this.dbName); return resolve(); }); }); }
const database = client.db(options.dbName);
process.exit(1) const torrentdb = mconn.db('torrent') const stream = torrentdb.collection('hash').find().stream() const d = []
process.exit(1) const torrentdb = mconn.db('torrent') const stream = torrentdb.collection('log').find({date: process.env.DATE}).stream() const d = []
process.exit(1) const torrentdb = mconn.db('torrent')
const database = client.db(options.dbName); if (!database) { delete this.connectionPromise;
_connect() { if (!this._connectionPromise) { this._connectionPromise = _mongodb.MongoClient.connect(this._databaseURI, this._mongoOptions).then(client => { this._client = client; return client.db(client.s.options.dbName); }); } return this._connectionPromise; }
_connect() { if (!this._connectionPromise) { this._connectionPromise = MongoClient.connect( this._databaseURI, this._mongoOptions ).then((client) => { this._client = client; return client.db(client.s.options.dbName); }); } return this._connectionPromise; }