function runProgramLogic() {
  var users = db.getCollection("users");
  var ov500 = users.getDynamicView("over 500");

  // generate random user and add
  var randomAge = Math.floor(Math.random() * 1000) + 1;
  var randomGender = Math.floor(Math.random() * 2);
  var newUser = {
    name : "user #" + users.count(),
    age : randomAge,
    gender : (randomGender?"m":"f")
  }
  users.insert(newUser);
  
  console.log("---- added user ----");
  console.log(newUser);
  console.log("");
  
  // lets get all female users in the users collection
  let result = users.chain("females").data();
  console.log("females:");
  console.log(result);
  console.log("");

  // let's use this within a chain
  result = users.chain().find({age: { $between: [300,700] } }).transform("females").data();
  console.log("females between 300-700 (empty initially) : ");
  console.log(result);
  console.log("");

  // if the 'females' transform filtered the results better, we might re-word this query as :
  result = users.chain("females").find({age: { $between: [300,700] } }).data();
  console.log("females between 300-700 (empty initially) : ");
  console.log(result);
  console.log("");
  
  
  // now let's use the transform as an extract for our dynamic view
  result = ov500.branchResultset("females").data();
  console.log("over 500 females : ");
  console.log(result);
  console.log("");
  
  // now let's hook up a paging algorithm to grab 1st page of 5 document page
  let page = 1,
    pageSize = 5,
    start = (page-1)*pageSize;

  // so transforms can be passed to chain() and transform() methods on collection as well
  // as to dynamic view's branchResultset()... in all instances you can pass parameters as an
  // optional parameter hash object.
  
  // call our parameterized transform as a dynamic view extract
  result = ov500.branchResultset("paged", { pageStart: start, pageSize: pageSize }).data();
  console.log("1st through 5th users over 500 : ");
  console.log(result);
  console.log("");

  // mix multiple transforms into a basic query chain...
  result = users
    .chain()
    .find({ age: { $gt: 200 } })
    .transform("females")
    .transform("paged", { pageStart: start, pageSize: pageSize })
    .data();
    
  console.log("first page (1-5) of females over 200");
  console.log(result);
  console.log("");
    
  console.log("press ctrl-c to quit");

}
Logic to run after database is initialized

Our example add 1 randomly generated user each time the program is run.

Let's run some transforms...