├── .gitignore ├── recording.gif ├── client ├── styles.css ├── index.html └── count_updater.js ├── package.json ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /recording.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/draperunner/mongo-live-count/master/recording.gif -------------------------------------------------------------------------------- /client/styles.css: -------------------------------------------------------------------------------- 1 | div { 2 | height: 100%; 3 | } 4 | 5 | #count { 6 | margin: 0; 7 | position: relative; 8 | top: 40%; 9 | text-align: center; 10 | font-size: 100px; 11 | } 12 | 13 | #storage-size { 14 | margin: 0; 15 | position: relative; 16 | top: 45%; 17 | text-align: center; 18 | color: grey; 19 | } 20 | -------------------------------------------------------------------------------- /client/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Count 4 | 5 | 6 | 7 | 8 |
9 |

10 |

11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /client/count_updater.js: -------------------------------------------------------------------------------- 1 | function numberWithCommas(x) { 2 | return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 3 | } 4 | 5 | setInterval(function () { 6 | fetch('/count') 7 | .then((res) => res.json()) 8 | .then((parsedResponse) => { 9 | document.getElementById("count").innerHTML = numberWithCommas(parsedResponse.count); 10 | document.getElementById("storage-size").innerHTML = "That's " + parsedResponse.storageSize; 11 | }) 12 | }, 1000); 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mongo-live-count", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "nodemon index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "author": "Mats Byrkjeland", 11 | "license": "MIT", 12 | "dependencies": { 13 | "bytes": "^3.1.0", 14 | "express": "^4.15.2", 15 | "mongodb": "^3.6.0" 16 | }, 17 | "devDependencies": { 18 | "nodemon": "^2.0.4" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MongoDB Live Count 2 | 3 | A very simple web app showing the total number of documents in a MongoDB collection live and its storage size. 4 | Count and storage size are updated every second. 5 | 6 | ![Screenshot GIF](recording.gif "Screenshot GIF") 7 | 8 | ### Get started 9 | ```bash 10 | git clone git@github.com:draperunner/mongo-live-count.git 11 | cd mongo-live-count 12 | npm install 13 | # Configure the app by setting environment variables. Defaults are shown below. 14 | export DB_ADDRESS=127.0.0.1 15 | export DB_PORT=27017 16 | export DB=test 17 | export COLLECTION=test 18 | export PORT=4321 # Server port 19 | ``` 20 | 21 | Start server by running `node index.js` 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const { MongoClient } = require("mongodb"); 4 | const bytes = require('bytes'); 5 | 6 | const dbAddress = process.env.DB_ADDRESS || '127.0.0.1'; 7 | const dbPort = process.env.DB_PORT || '27017'; 8 | const db = process.env.DB || 'test'; 9 | const collection = process.env.COLLECTION || 'test'; 10 | const port = process.env.PORT || 4321; 11 | 12 | const mongoAddress = 'mongodb://' + dbAddress + ':' + dbPort + '/' + db 13 | let client 14 | 15 | app.use(express.static('client')) 16 | 17 | app.get('/count', async (req, res) => { 18 | try { 19 | if (!client) { 20 | client = await MongoClient.connect(mongoAddress) 21 | } 22 | 23 | const [count, stats] = await Promise.all([ 24 | client.db().collection(collection).estimatedDocumentCount(), 25 | client.db().collection(collection).stats(), 26 | ]) 27 | 28 | res.json({ 29 | count: count, 30 | storageSize: bytes.format(stats.storageSize, { unitSeparator: ' ' }) 31 | }); 32 | } catch (error) { 33 | console.error(error) 34 | res.status(500).json({ error: error.message }) 35 | } 36 | }); 37 | 38 | app.listen(port, () => { 39 | console.log('Connected to MongoDB instance ' + mongoAddress + ', collection "' + collection + '"'); 40 | console.log('Server listening on port ' + port); 41 | }); 42 | --------------------------------------------------------------------------------