Code Examples

The following are simple Javascript code snippets to Create, Modify, & Remove a Document in Datanet. NodeJS & Browser Javascript have the same API

Create Document:
    * Namespace defaults to 'production'
    * Collection is 'users'
    * Document key is 'Tim'
var ZCmdClient = require('./zcmd_client');
var Client     = ZCmdClient.ConstructCmdClient();
var collection = Client.Collection("users");
var username   = "Tim";
var data       = {'_id'     : username,
                  'age'     : 22,
                  'hobbies' : ["reading", swimming"]
                 };
collection.store(data, function(err, res) {
  if (err) console.error(err.message);
  else {
   console.log('USER DATA STORED');
  }
});
        
Modify Document:
    * Namespace defaults to 'production'
    * Collection is 'users'
    * Document key is 'Tim'
    * Actions are to increment Tim's "age" by one (it's his birthday) and add hobby 'sailing'
    * NOTE: for brevity, error checking has been removed
var ZCmdClient = require('./zcmd_client');
var Client     = ZCmdClient.ConstructCmdClient();
var collection = Client.Collection("users");
var username   = "Tim";
collection.fetch(username, function(err, doc) {
  doc.incr("age", 1);
  doc.rpush("hobbies", "sailing");
  doc.commit(function(err, doc) {
      console.log('HAPPY BIRTHDAY');
  });
});
        
Remove Document:
    * Namespace defaults to 'production'
    * Collection is 'users'
    * Document key is 'Tim'
var ZCmdClient = require('./zcmd_client');
var Client     = ZCmdClient.ConstructCmdClient();
var collection = Client.Collection("users");
var username   = "Tim";
collection.remove(username, function(err, ok) {
  if (err) console.error(err.message);
  else {
    console.log('USER DATA REMOVED');
  }
});
        
Extended Documentation
Javascript API: Datanet NodeJS API