Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 70b7783fadeaac517be4c359d8701e5ca5745f5a Kristiono Setyadi committed Oct 20, 2011
Showing with 64 additions and 0 deletions.
  1. +20 −0 README.md
  2. +44 −0 node_mongo.js
@@ -0,0 +1,20 @@
+Example use of NodeJS and MongoDB
+=================================
+
+Intruction:
+-----------
+
+To run the script, you can type:
+
+ $ node node_mongo.js
+
+You can always see the data changes by using MongoDB.
+To start MongoDB on your local server, run it as a daemon:
+
+ $ sudo mongodb
+
+and then run the MongoDB console:
+
+ $ mongo
+
+You will then be given a MongoDB prompt.
@@ -0,0 +1,44 @@
+var Db = require('mongodb').Db;
+var Server = require('mongodb').Server;
+
+var client = new Db('test', new Server('127.0.0.1', 27017, {}));
+
+var insertData = function(err, collection) {
+ collection.insert({name: "Kristiono Setyadi"});
+ collection.insert({name: "Meghan Gill"});
+ collection.insert({name: "Spiderman"});
+}
+
+var removeData = function(err, collection) {
+ collection.remove({name: "Spiderman"});
+}
+
+var updateData = function(err, collection) {
+ collection.update({name: "Kristiono Setyadi"}, {name: "Kristiono Setyadi", sex: "Male"});
+}
+
+var listAllData = function(err, collection) {
+ collection.find().toArray(function(err, results) {
+ console.log(results);
+ });
+}
+
+var findName = function(err, collection) {
+ console.log("Find Name...");
+ collection.find({name: "Kristiono Setyadi"}).toArray(function(err, results) {
+ console.log(results);
+ });
+}
+
+client.open(function(err, pClient) {
+ client.collection('test_insert', insertData);
+
+ client.collection('test_insert', removeData);
+
+ client.collection('test_insert', updateData);
+
+ client.collection('test_insert', listAllData);
+
+ client.collection('test_insert', findName);
+});
+

0 comments on commit 70b7783

Please sign in to comment.