开发者社区> 问答> 正文

mongodb高级修改问题

描述: 格式如下所示,其中每个对象有_id,name,和一个数组scores,其中可以看到修改前的数组中,每个document有两个type为"homework"的对象。
提问: 问题是如何操纵mongo数据库,批量修改db.students,让每个document中,删除score较小的homework,而保留score较大的homework。

修改前:

    "_id" : 100,
    "name" : "Demarcus Audette",
    "scores" : [
        {
            "score" : 47.42608580155614,
            "type" : "exam"
        },
        {
            "score" : 44.83416623719906,
            "type" : "quiz"
        },
        {
            "score" : 19.01726616178844,
            "type" : "homework"
        },
        {
            "score" : 39.01726616178844,
            "type" : "homework"
        }
    ]
}

修改后:

{
    "_id" : 100,
    "name" : "Demarcus Audette",
    "scores" : [
        {
            "score" : 47.42608580155614,
            "type" : "exam"
        },
        {
            "score" : 44.83416623719906,
            "type" : "quiz"
        },
        {
            "score" : 39.01726616178844,
            "type" : "homework"
        }
    ]
}

下面附上一段nodejs上跑的代码(自己写的,有问题跑不通,作为参考):

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/school', function(err, db){
    if(err) throw err;
    var query = {};
    var cursor = db.collection('students').find(query);
    cursor.each(function(err, doc){
        if(err)throw err;
        if(doc == null){return db.close();}

        /*TODO*/

        var target1 = doc.scores[2];
        var target2 = doc.scores[3];
        if(target1 < target2) doc.update({$unset: target1});
            else doc.update({$unset: target2});


        console.dir("Successfully found " + target1);
    });
});

展开
收起
a123456678 2016-06-29 10:17:15 2327 0
1 条回答
写回答
取消 提交回答
  • var MongoClient = require('mongodb').MongoClient;
    
    MongoClient.connect('mongodb://school:school@localhost:27017/school', function(err, db) {
      if (err) {
        throw err;
      }
    
      var student = db.collection('students');
      var updateData = function(newdoc) {
        //把旧的删除
        student.findAndRemove({_id: newdoc._id}, function(err, olddoc) {
          if (err) {
            throw err;
          }
    
          olddoc && console.log('remove olddoc id: %s', olddoc._id);
    
          //插入新的
          student.insert(newdoc, function(err, saveResult) {
            if (err) {
              throw err;
            }
    
            saveResult && console.log('[OK]  update ok , id: %s', newdoc._id);
            saveResult || console.log('[ERR] update fail, id: %s', newdoc._id);
          });
    
        });
      };  
    
      //插入测试数据
      student.insert([
        {
          name: 'hehehe',
          scores: [
            {
              score: 97.42608580155614,
              type: 'exam',
            },
            {
              score: 14.83416623719906,
              type: 'quiz',
            },
            {
              score: 55.01726616178844,
              type: 'homework',
            },
            {
              score: 3.0172661617884,
              type: 'homework',
            }
          ],
        },    
        {
          name: 'Demarcus Audette',
          scores: [
            {
              score: 47.42608580155614,
              type: 'exam',
            },
            {
              score: 44.83416623719906,
              type: 'quiz',
            },
            {
              score: 19.01726616178844,
              type: 'homework',
            },
            {
              score: 39.0172661617884,
              type: 'homework',
            }
          ],
        },
      ], function(err, result) {
        if (err) {
          throw err;
        }
    
        //聚合
        student.aggregate([
          {$unwind: '$scores'},
          {$group: {
            '_id': {
              '_id': '$_id',
              name: '$name',
              type: '$scores.type',
            },
            score: {
              '$max': '$scores.score'
            }
          }},
          {$project: {
            '_id': {
              _id: '$_id._id',
              name: '$_id.name',
            },
            scores: {
              type: '$_id.type',
              score: '$score',
            },
          }},
          {$group: {
            '_id': '$_id',
            scores: {
              '$push': '$scores',
            }
          }}
        ], function(err, result) {
          if (err) {
            throw err;
          }
    
          //循环结果
          result.forEach(function(item) {
            item = {
              _id: item._id._id,
              name: item._id.name,
              score: item.scores
            };
            updateData(item);
          });
        });    
    
      });
    
    
    });
    2019-07-17 19:48:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Data as a Service - 数据即服务 -- MongoDB⾼级应⽤模式 立即下载
开源数据库 MongoDB 专场 MongoDB疑难杂症分析及优化 立即下载
饿了么高级架构师陈东明:MongoDB是如何逐步提高可靠性的 立即下载