Advertisement
dmontal2

pozr_nodejs_mongodb

Apr 24th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //POZR MONGODB SERVICES CONNECTION STRING MOD033014
  2. // QUERY BASED ON SQL FILE CONTAINED IN SAMPLES FOLDER
  3. // IMPORT SQL INTO SQL DATABASE "db.pozr.in"  THEN IMPORT INTO MONGODB
  4. var mongo = require('mongodb');
  5. var dbname = 'california';//YOUR POZR HANDLE
  6. var dbuser = 'pozr-username';//POZR-YOURNAME
  7. var dbkey = '';//PASSWORD GENERATED BY POZR SERVICES
  8. var server = new mongo.Server('no.pozr.in', 27017);
  9. db = new mongo.Db(dbname, server, {fsync:true});
  10. db.open(function(err, db) {
  11.     if(!err) {
  12.         console.log("Connected to database");
  13.         db.authenticate(dbuser, dbkey, function(err, res) {
  14.             if(!err) {
  15.                  console.log("Authenticated");
  16.                
  17.                 //SPECIFIC COLLECTION
  18.                 var collection = db.collection('uCounter');
  19.                
  20.                 //INSERT RECORD INTO COLLECTION
  21.                 var record = 'pozrapps' + Math.floor((Math.random()*100)+1);
  22.                 collection.insert({count_property: record}, function(docs) {
  23.                    
  24.                  var inserted = 'inserted into db -> ' + record;
  25.                  console.log(inserted);
  26.                    
  27.                 });
  28.                                  
  29.                 //COUNT TOTAL RECORDS
  30.                 collection.count(function(err, count) {
  31.                 console.log("db contains -> " + count + " records.");
  32.                 });
  33.                
  34.                 //RETREIVE ONE RESULT                
  35.                 var query = { 'count_property' : 'java1' };
  36.                 collection.findOne(query, function(err, item) {
  37.                         console.log('viewing one record ->');
  38.                         console.log(item);
  39.                 });
  40.                
  41.                
  42.                 //RETREIVE ALL UNCOMMENT
  43.                 /*collection.find().each(function(err, doc) {
  44.                 if(doc != null) console.log("Doc from Each ");
  45.                 console.log(doc);
  46.                 });*/
  47.                
  48.                
  49.                
  50.                
  51.             } else {
  52.                 console.log("Error in authentication.");
  53.                 console.log(err);
  54.             }
  55.         });
  56.     } else {
  57.         console.log("Error in open().");
  58.         console.log(err);
  59.     };
  60. });
  61.  
  62. //READ MORE ABOUT THIS MODULE
  63. // http://mongodb.github.io/node-mongodb-native/contents.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement