1

Hi im new to nodejs and mongodb What I want to do after executing my aggregate command is to drop the lowest score that the aggregate command has found.. This is my code:

MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
    if(err) throw err;

    var students = db.collection("students");

    students.aggregate( [ { "$unwind": "$scores" },
    {"$group": { '_id':'$_id' , 'score': {'$min': "$scores.score" } } }],function(err, result){
            if(err) throw err;
            for(var i=0; i<200; i++){
                //console.log(result[i].score);
                updateStudents(result[i]._id,result[i].score);
            }
    });


    function updateStudents(_id,score){
        var query = {'_id':_id};
        var operator = {'$unset' : {'scores.score' : score}};
        var options = {multi : true};

        students.update(query,operator,options,function(err,updated){
            if(err) throw err;
            console.dir('Successfully Updated' + updated);
        });
    }

});

It says that it has updated successfully but when I check it via query, it has not dropped the lowest score of the users.

this is what the schema looks like:

db.students.find({'_id':0}).pretty()
{
    "_id" : 0,
    "name" : "aimee Zank",
    "scores" : [
        {
            "type" : "exam",
            "score" : 1.463179736705023
        },
        {
            "type" : "quiz",
            "score" : 11.78273309957772
        },
        {
            "type" : "homework",
            "score" : 6.676176060654615
        },
        {
            "type" : "homework",
            "score" : 35.8740349954354
        }
    ]
}

I would like to drop the first element which has the lowest score but cant

0

1 Answer 1

2

Your function updateStudents set up a callback which close the connection return db.close();

So obviously if one update finishes and call the callback before you have made all updates, you have this MongoError: Connection Closed By Application error.

You should not close connection after request in a node.js app. You should use always the same connection, passing it through your application to all modules needing it.

To remove value from array, you should not use $unset which is use to drop entire fields. You should use : var operator = {'$pull' : {'scores' : {'score' : score} } }; which will remove the value score from the array scores.

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

1 Comment

hi, thanks for the reply. I have updated my question, the error was gone but it seems my update is still not working properly. It says it has updated but when I checked the db via query it did not $unset any of the score that has the lowest score to the users. why is this?

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.