Streams data in capped collection and push to client.
- Download Nodejs from http://nodejs.org/. In Ubuntu 13.04 you can install using apt-get
sudo apt-get install nodejs
If you install Node.js via
apt-get
you can run Node.js application usingnodejs
command. Sometime, some module run as Bash Shell that usenode
command, you may create symbolic link fromnodejs
tonode
to avoid command not found error.
- Download source code of this project from https://github.com/khasathan/nodejs-mongodb-stream. However, you can clone by git
git clone https://github.com/khasathan/nodejs-mongodb-stream.git
Install requires modules for this project. you can use NPM (Nodejs Package Manager)
cd node-mongodb-stream
npm install
NPM will be install specific modules in package.json file. After install you should have node_modules directory.
Collection we will use MUST BE capped collection so it's use stream feature. You can create capped collection by mongodb command
db.createCollection('col_name', { capped : true, size : 100000, max : 10 });
Or you want to convert exist collection to capped
db.runCommand({'convertToCapped' : 'col_name', size : 100000, max : 10 });
Check the collection you create is capped
db.col_name.isCapped();
- capped (boolean) set collection to capped
- size (int) size of colleciton in byte
- max (int) max document in collection
NOTE: The capped collection provide to use in some case like real-time push service. It's NOT used for data storage because old data in capped collection will be deleted automatically. If you want store data, you can insert data into general collection too. See MongoDB document about Capped Collection here http://docs.mongodb.org/manual/core/capped-collections
- Start Mongodb first
- Running Nodejs server
cd node-mongodb-stream
node app.js
- Open index.html in web browser
- Insert document into MongoDB message should be appear in browser