Tag:

更新嵌套树mongoDB,node.js中的后代

有没有办法通过ID或其他字段更新嵌套的文件? 我使用“ 单一文件中的完整树 ”,事先不知道嵌套深度可以达到多less。 需要更新,例如,用{id:'104'}回答。 我可以通过'点符号'来做到这一点,但是因为我不知道嵌套的级别(深度),所以我无法预测我的'comment.answers.answers….answers.'多久'comment.answers.answers….answers.' 可以去。 有什么办法可以直接find并更新id:'104',还是需要通过某种深度标记? { title:'some title', comment: { id:'101' author:'Joe', text:'some comment', answers: [ { id:'102' author:'Joe', text:'first answer to comment', answers: [ { id:'103' author:'Done', text:'first answer to first answer to comment', answers:[] }, { id:'104' author:'Bob', text:'Second answer to first answer to comment', answers:[] } ] }, { […]

Javascript:涉及I / O的定向树的DFS遍历

给定一个定向树T,每个节点的子节点数可变,我想find一个从根开始的“好”节点的PATH_SIZE大小的path。 每个节点都有一个isGood()方法和一个按照预期工作的getChildren()方法。 一个简单的DFSrecursion解决scheme将如下所示:(请纠正我,如果我错了) function findGoodPath(node, depth){ if(!node.isGood()){ return null; } else if (depth==PATH_SIZE){ return [node]; } var children = node.getChildren(); for (var i=0; i<children.length; i++){ var result = findGoodPath(children[i], depth+1); if (result){ return result.concat([node]); } } return null; } 调用findGoodPath(root, 1)应该find一个结果,如果存在的话。 现在的问题是 :节点对象的getChildren()方法实际上是一个asynchronous方法,在后台执行I / O操作。 它什么都不返回,并期望一个callback参数来处理返回的孩子。 修改后的代码解决scheme(这是错误的 )可能看起来像这样: function findGoodPath(node, depth){ if(!node.isGood()){ return null; } […]

在mongoDB中迭代树

我有这样的数据的集合(例如): { name : "john" , _id : "0" }, { name : "Richard" , parent_id : "0" , _id : "1" }, { name : "Kevin" , parent_id : "0" , _id : "2" }, { name : "William" , parent_id : "1" , _id : "3" }, { name : "George" , parent_id […]

在JavaScript中将文件/目录结构转换为“树”

我有一个像这样的对象数组: [{ name: 'test', size: 0, type: 'directory', path: '/storage/test' }, { name: 'asdf', size: 170, type: 'directory', path: '/storage/test/asdf' }, { name: '2.txt', size: 0, type: 'file', path: '/storage/test/asdf/2.txt' }] 可以有任意数量的任意path,这是遍历目录中的文件和文件夹的结果。 我想要做的是确定这些的“根”节点。 最终,这将被存储在MongoDB中,并使用物化path来确定它的关系。 在这个例子中, /storage/test是一个没有父节点的根。 /storage/test/asdf具有/storage/test的父级,它是/storage/test/asdf/2.txt的父级。 我的问题是,你将如何去遍历这个数组,以确定父母的和相关的孩子? 任何帮助正确的方向将是伟大的! 谢谢

在javascript中将数组转换为嵌套对象

我有典型的组织hierarchy 。 例如。 D,E is reporting to B. B,C is reporting to A. A是最顶端的节点。 但是,我收到这个数据作为一个平面数组与指向父母的属性。 [{ name: "A", parent: null }, { name: "B", parent: "A" }, { name: "C", parent: "A" }, { name: "D", parent: "B" }, { name: "E", parent: "B" }] 但我想将其转换为single nested object或tree 。 根节点具有儿童embedded的子属性,每个子节点都有自己的子属性。 { name: "A", children: [{ […]

使用async.js进行asynchronous树遍历

我试图使用async.js遍历一个嵌套的项目树。 遍历一个分支后,遍历终止。 var count=0; exports.buildFamily = function(item_id, mback){ var extendedFamily={}; exports.getItembyId(item_id, function(err, item){ extendedFamily=item; if(item.descendants){ extendedFamily.kids=[]; count=+item.descendants.length; console.log('outercount ' + count); async.eachSeries(item.descendants, function(item){ count– console.log('item: ' + item) exports.buildFamily(item, function(err, family){ console.log('deepcount: ' + count); extendedFamily.kids.push(family); if(count===0){ return mback(null, extendedFamily);} else {extendedFamily.kids.push(family);} }) }) } else{ if(count===0){ return mback(null, extendedFamily);} else{ extendedFamily.kids.push(family); return; } } […]