Note that there are some explanatory texts on larger screens.

plurals
  1. POCollect data recursively in nodejs and then delete it one by one
    text
    copied!<p>infom_col is a collection which contains a field rkey which itself contains an array like this: rkeys -> brands so brands is an array of ids. These ids are reference to other documents in the collection. I need to collect all such dependent ids and delete the documents one by one. </p> <p>This is my code. Throws an error: TypeError: Cannot read property 'rkeys' of undefined</p> <pre><code>function collect_data(id) { db.infom_col.find({"_id":id}, function(err,doc) { if(err) throw err; else { console.log("else: " + doc._id); console.log("length: " + doc.rkeys.length); if(typeof(doc[0].rkeys) != 'undefined' &amp;&amp; doc[0].rkeys != null) { for(key in doc[0].rkeys){ doc[0].rkeys[key].forEach(function(entry) { //console.log("entry: " + entry); ids.push(entry); //console.log(ids); }); } if(ids.length &gt; 0) { var id1 = ids.pop(); console.log(id1); db.infom_col.remove({"_id":id1}, function(err,doc1) { if(err) throw err; else { console.log("deleted: " + id1); var id2 = ids.shift(); console.log("going to delete: " + id2); collect_data(id2,null); } }); } } } }); } </code></pre> <p>Update1: </p> <pre><code>function collect_data(id,cb) { db.infom_col.findOne({"_id":id}, function(err,doc) { if(err) throw err; else { console.log("else: " + doc._id); if (doc.rkeys) { console.log("length: " + doc.rkeys.length); for(key in doc.rkeys){ doc.rkeys[key].forEach(function(entry) { console.log("entry: " + entry); ids.push(entry); }); } if(ids.length &gt; 0) { var id1 = ids.shift(); console.log("id1: " + id1); db.infom_col.remove({"_id":id1}, function(err,doc1) { if(err) throw err; else collect_data(id1,null); }); } } } }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload