0

我对学生集合有一个聚合查询,该集合为每个学生返回两组结果,如下所示

{ _id: 1543,
  name: 'Bill Jackson',
  scores: { type: 'homework', score: 38.86823689842918 } }

{ _id: 1543,
  name: 'Bill Jackson',
  scores: { type: 'homework', score: 15.861613903793295 } }

这工作正常。现在在回调中,我想删除每个学生的一个分数。我在下面使用丑陋的嵌套条件来隔离我要删除的两条记录中的哪一条,一旦实现,我创建一个查找和修改查询来删除文档,但没有证据表明它正在运行。findAndModify 的错误或成功回调都没有运行,但是我能够记录我在调用 findAndModify 的区域内。

是否可以在聚合回调中查询数据库?如果没有,我应该如何执行在数据库中持续存在的操作?

                              //aggregation query ommitted
  , function(err, result) {   //callbackstarts here with result of aggregation query that returns two records for each student


   for (var i=0; i<result.length; i++) {

    var id = result[i]['_id'];

    if (id === result[i]['_id']){
        if (foo && foo === result[i]['_id']){
         //if we're in here, we know we need to remove score associated with this result[i]['_id']

          //create findAndModify to remove the record
                var query = { '_id' : result[i]['_id']}
                var sort = []
                var operation = { '$pull' : { 'scores.score' : result[i]['scores']['score'] } };
                var options = []

                console.log('this code is getting called but findAndModify not')

                db.collection('students').findAndModify(query, sort, operation, options,function(err, doc) {
                            if(err) throw err;

                            if (!doc) {
                                console.log("record not found");
                            }
                            else {
                                console.log("changed doc" + doc);
                            }


                        });

        }else {

          var foo = result[i]['_id']  //part of logic to isolate which of two records to remove


           }
4

0 回答 0