Skip to content

Instantly share code, notes, and snippets.

@TalhaAwan
Last active August 3, 2017 21:02
Show Gist options
  • Save TalhaAwan/81947b63c9d845af338295b2f37037c7 to your computer and use it in GitHub Desktop.
Save TalhaAwan/81947b63c9d845af338295b2f37037c7 to your computer and use it in GitHub Desktop.
Couchbase, Node js. Find family tree, recursively collect ids of its leaf nodes, and get all leaf node documents with getMulti.
var couchbase = require("couchbase");
var cluster = new couchbase.Cluster("127.0.0.1:8091");
var CbBucket = "aBucket";
var pass = "12345678";
var bucket = db.openBucket(CbBucket, password, function(err) {
bucket.operationTimeout = 500 * 1000; //increase connection time
if(err){
throw err;
}
else{
bucket.get("Family_6da5b3a7-5ab6-40a3-b0b4-95cb32237978", function(err, family){
if (err){
throw err;
}
else if (!family.value.tree || family.value.tree.nodes.length == 0){
console.log("No family tree found");
}
else{
var allNodes = [];
function getNodes(nodes){
nodes.forEach(function(node){
if(node.nodes.length == 0){
allNodes.push(node.id);
}
getNodes(node.nodes);
})
}
getNodes(family.value.tree.nodes);
bucket.getMulti(allNodes, function (err, leafNodes) {
if (err) {
throw err;
}
else {
console.log(leafNodes);
}
})
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment