开发者社区 问答 正文

mongodb高级修改问题

描述: 格式如下所示,其中每个对象有_id,name,和一个数组scores,其中可以看到修改前的数组中,每个document有两个type为"homework"的对象。
提问: 问题是如何操纵mongo数据库,批量修改db.students,让每个document中,删除score较小的homework,而保留score较大的homework。
修改前:
screenshot
修改后:
screenshot
下面附上一段nodejs上跑的代码(自己写的,有问题跑不通,作为参考):
screenshot

展开
收起
蛮大人123 2016-02-14 13:42:32 2288 分享 版权
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 18:42:34
    赞同 展开评论