Skip to content

Instantly share code, notes, and snippets.

View dmcassel's full-sized avatar

David Cassel dmcassel

View GitHub Profile
/*
* Find the active users who mention "sell out" in their profile. Of the three
* most recent registrants, order by most number of tags.
*/
var query = cts.andQuery([
cts.orQuery([
cts.jsonPropertyValueQuery("isActive", true),
cts.jsonPropertyWordQuery("about", "sell out") // The text "sell out" or its stems
]),
/*
* Insert some JSON generated from http://www.json-generator.com and http://hipsum.co, natch.
*/
// Tell the transaction manager that we intend to update the database.
declareUpdate();
var data = [
{
"id": 0,
// utilities.sjs
// Library modules can require other library modules.
var o = require("../other.sjs");
module.exports = {
// Maps the internal implementation to a public name.
doSomething: internalSomething
};
// Main module
var util = require("utilities.sjs");
util.doSomething();
function* asObjects(itr) {
for(var doc of itr) {
yield doc.toObject();
}
}
var a = [];
for(var obj of asObjects(fn.collection())) {
// Assumes you have JSON documents in the database with .name properties, as in the examples below.
a.push(obj.name = "Generated: " + obj.name);
var itr = fn.collection();
while (true) { // Loop until break
var item = itr.next(); // Advance the iterator
if(item.done) break; // If we're at the end, exit the loop
var user = item.value.toObject();
// Do something with the object...
}
// fn.collection(), and most things that interact with the database, returns a ValueIterator
// The ES6 for…of loop automatically iterates.
for (var doc of fn.collection()) {
var user = doc.toObject();
// Do something with the object...
}
// We need to call declareUpdate() to persist changes in the database.
declareUpdate();
var uri = "/n81ff186a6c5290db.json";
// cts.doc() returns a Document node or null, given a unique id (URI)
var user = cts.doc(uri)
.root // The root Node instance of the Document container. For a JSON document, likely an ObjectNode
.toObject(); // Turn any Node instance into JavaScript object.
declareUpdate();
var user = {
"name": "Dave Cassel"
};
xdmp.documentInsert(
'/dave-cassel.json',
user,
xdmp.defaultPermissions(),
{
"user": {
"personal": {
"userID": 38465,
"title": "Tech Adventurer"
},
"experiences": [
{
"title": "Lead Adventurer",
"text": "experience description",