Node.js 查找、修改和删除递归嵌入式文档mongoosejs

Node.js 查找、修改和删除递归嵌入式文档mongoosejs,node.js,mongoose,Node.js,Mongoose,我使用示例中的方案描述 Comment.add({ title : { type: String, index: true } , date : Date , body : String , comments : [Comment] }); var BlogPost = new Schema({ title : { type: String, index: true } , slug : { type: Strin

我使用示例中的方案描述

Comment.add({
    title     : { type: String, index: true }
  , date      : Date
  , body      : String
  , comments  : [Comment]
});

var BlogPost = new Schema({
    title     : { type: String, index: true }
  , slug      : { type: String, lowercase: true, trim: true }
  , date      : Date
  , buf       : Buffer
  , comments  : [Comment]
  , creator   : Schema.ObjectId
});
我在评论中有几个层次的嵌套。 如何在任何嵌套级别找到正确的注释,并对其执行任何操作(删除、编辑或添加新的嵌套注释) 我试图对搜索进行递归,但您无法保存或删除注释

BlogPost.methods.findComment = function (id, callback) {

  var curentComment = this.comments;
  var findComment = null;
  var recursiveFindComment = function(comment){
      for(i=0;i<comment.length;i++){
          if(findComment){
              break;
          }
          if(comment[i]._id == id){
              findComment  = comment[i];
              break;    
          }else if(comment[i].comments.length>0){
              findComment = recursiveFindComment(comment[i].comments)
          }
      }
      return findComment;

  }

  if(curentComment.id(id)){
     callback(curentComment);  
  }else{
     callback(recursiveFindComment(curentComment, null)) 
  }
}
BlogPost.methods.findComment=function(id,回调){
var curentComment=this.comments;
var findComment=null;
var recursiveFindComment=函数(注释){
对于(i=0;i0){
findComment=recursiveFindComment(注释[i]。注释)
}
}
返回findComment;
}
if(curentComment.id(id)){
回调(curentComment);
}否则{
回调(recursiveFindComment(currentComment,null))
}
}

您可能希望了解如何在MongoDb()中使用树。有许多不同的方法可以帮助解决模式的递归性质

> t = db.tree
test.tree

> // get entire tree -- we use sort() to make the order nice
> t.find().sort({path:1})
{ "_id" : "a", "path" : "a," }
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }
{ "_id" : "e", "path" : "a,e," }
{ "_id" : "f", "path" : "a,e,f," }
{ "_id" : "g", "path" : "a,b,g," }

> t.ensureIndex( {path:1} )

> // find the node 'b' and all its descendents:
> t.find( { path : /^a,b,/ } )
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }

> // find the node 'b' and its descendents, where path to 'b' is not already known:
> nodeb = t.findOne( { _id : "b" } )
{ "_id" : "b", "path" : "a,b," }
> t.find( { path : new RegExp("^" + nodeb.path) } )
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }

您可能想看看在MongoDb()中使用树。有许多不同的方法可以帮助解决模式的递归性质

> t = db.tree
test.tree

> // get entire tree -- we use sort() to make the order nice
> t.find().sort({path:1})
{ "_id" : "a", "path" : "a," }
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }
{ "_id" : "e", "path" : "a,e," }
{ "_id" : "f", "path" : "a,e,f," }
{ "_id" : "g", "path" : "a,b,g," }

> t.ensureIndex( {path:1} )

> // find the node 'b' and all its descendents:
> t.find( { path : /^a,b,/ } )
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }

> // find the node 'b' and its descendents, where path to 'b' is not already known:
> nodeb = t.findOne( { _id : "b" } )
{ "_id" : "b", "path" : "a,b," }
> t.find( { path : new RegExp("^" + nodeb.path) } )
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }