classExists(name) { return this.connect().then(() => { return this.database.listCollections({ name: this._collectionPrefix + name }).toArray(); }).then(collections => { return collections.length > 0; }).catch(err => this.handleError(err)); }
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'); })); }); });
beforeEach(async () => { const gsAdapter = new GridStoreAdapter(databaseURI); const db = await gsAdapter._connect(); await db.dropDatabase(); });
it('handleShutdown, close connection', async () => { const databaseURI = 'mongodb://localhost:27017/parse'; const gfsAdapter = new GridFSBucketAdapter(databaseURI); const db = await gfsAdapter._connect(); const status = await db.admin().serverStatus(); expect(status.connections.current > 0).toEqual(true); await gfsAdapter.handleShutdown(); try { await db.admin().serverStatus(); expect(false).toBe(true); } catch (e) { expect(e.message).toEqual('topology was destroyed'); } });
executeInCollection(name, fn) { return new Promise((resolve, reject) => { if (!this.db) { return reject( new Error( 'It seems that mongodb is not initialized, try invoking connect()' ) ); } const collection = this.db.collection(name); return fn(collection, (err, result) => { if (err) { return reject(err); } return resolve(result); }); }); }
it('handleShutdown, close connection', async () => { const adapter = new MongoStorageAdapter({ uri: databaseURI }); const schema = { fields: { array: { type: 'Array' }, object: { type: 'Object' }, date: { type: 'Date' }, }, }; await adapter.createObject('MyClass', schema, {}); const status = await adapter.database.admin().serverStatus(); expect(status.connections.current > 0).toEqual(true); await adapter.handleShutdown(); try { await adapter.database.admin().serverStatus(); expect(false).toBe(true); } catch (e) { expect(e.message).toEqual('topology was destroyed'); } });
const stream = torrentdb.collection('hash').find().stream() const d = [] stream.on('error', (err) => {
_adaptiveCollection(name) { return this.connect().then(() => this.database.collection(this._collectionPrefix + name)).then(rawCollection => new _MongoCollection.default(rawCollection)).catch(err => this.handleError(err)); }
}); await adapter.connect(); await adapter.database.dropDatabase(); delete adapter.connectionPromise;
const stream = torrentdb.collection('log').find({date: process.env.DATE}).stream() const d = [] let ii = 0 stream.pause() console.log(ii) torrentdb.collection('hash').bulkWrite(d, (err, r) => { if(err) { console.error(err) stream.on('finish', () => { if(d.length > 0) { torrentdb.collection('hash').bulkWrite(d, (err) => { if(err) { console.error(err)
const gridStoreAdapter = new GridStoreAdapter(databaseURI); const db = await gridStoreAdapter._connect(); await db.dropDatabase(); const filesController = new FilesController( gridStoreAdapter, .collection('fs.files') .count() .then(count => { .collection('fs.chunks') .count() .then(count => { .collection('fs.files') .count() .then(count => { .collection('fs.chunks') .count() .then(count => { const status = await db.admin().serverStatus(); expect(status.connections.current > 0).toEqual(true); await db.admin().serverStatus(); expect(false).toBe(true); } catch (e) {
const stream = torrentdb.collection('log').aggregate([
async function find(collectionname,json) { const client = new MongoClient(url, {useUnifiedTopology: true}); await client.connect(); const col = client.db("spider").collection(collectionname); const result = await col.find(json).toArray(); client.close(); return result; }