1

I am writing a cleansing function which will delete all invalid docs from collection. For this I have an idea to push invalid _id values into array variable and delete with $in.

function(scan){
var err
for(var n=1;n<scan;n++){
var doc = db.zeroDimFacts.findOne({_id:n}) ,nKeys =0; 
for(k in doc)
    nKeys++
if(nKeys <5)
    err = n.toArray()
}

After I push all values to err Array, I have a script to delete matching docs. However, there is something missing with the code which throws me error at n.toArray() .
Can someone help me to correct the code?

7
  • what is the criteria here to find the invalid docs ? Commented Jul 14, 2016 at 7:54
  • as mentioned in the problem statement, criteria is to delete the invalid docs. Commented Jul 14, 2016 at 8:13
  • Not clear what is an invalid doc. If you explain it someone can help you do it in a criteria. Commented Jul 14, 2016 at 8:29
  • Hi Sherin, If you are asking how I assume a doc as invalid, it a simple check to count the number of keys, whereas it should be 5 in my case, if the keys are less then it fails in the criteria. Commented Jul 14, 2016 at 8:32
  • For this scope, a doc is considered as invalid if the keys are less than 5. If you see the code, I am trying to insert the _id values of docs which are having less than 5 keys. In short, I don't want to immediately delete a doc if it is invalid, I want to push all the values into array and then delete in go at the end. Commented Jul 14, 2016 at 8:35

2 Answers 2

1
function(scan) {
    var doc;
    var nKeys;
    var err = [];

    for(var n = 1; n < scan; ++ n) {
        doc = db.zeroDimFacts.findOne({_id: n})
        nKeys = 0;

        for(k in doc) {
            ++ nKeys;
        }
        if(nKeys < 5) {
            err.push(n);
        }
    }
    return err;
};

Pay attention to findOne() call. When it returns null, n will get into the array which seems undesirable to me.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use findByIdAndRemove({criteria}) and depending on return value you can have the logic.

8 Comments

Hi, I am not working on mongoose, I am writing a function inside mongodb.
I'm not sure on this, it may help in general scenario because it validates and delete the document in very successful iteration, it would take a longer time for a collection having millions of docs. This is the reason why I have opted to first collect all the id of invalid docs and then input to delete them in one go.
Inserting in a bulk way is faster because before insertion all your docs are in memory and they are inserted to db->storage. But in delete case they are in db already - you can use db.collection.remove with some criteria. Collecting all ids and deleting them as you have explained might not be the solution you are looking for.
Also have a look at this - if it helps: stackoverflow.com/questions/28788841/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.