为了账号安全,请及时绑定邮箱和手机立即绑定

mongodb高级修改问题

mongodb高级修改问题

眼眸繁星 2019-04-08 11:19:00
描述:格式如下所示,其中每个对象有_id,name,和一个数组scores,其中可以看到修改前的数组中,每个document有两个type为"homework"的对象。*提问*:问题是如何操纵mongo数据库,批量修改db.students,让每个document中,删除score较小的homework,而保留score较大的homework。修改前:{"_id":100,"name":"DemarcusAudette","scores":[{"score":47.42608580155614,"type":"exam"},{"score":44.83416623719906,"type":"quiz"},{"score":19.01726616178844,"type":"homework"},{"score":39.01726616178844,"type":"homework"}]}修改后:{"_id":100,"name":"DemarcusAudette","scores":[{"score":47.42608580155614,"type":"exam"},{"score":44.83416623719906,"type":"quiz"},{"score":39.01726616178844,"type":"homework"}]}下面附上一段nodejs上跑的代码(自己写的,有问题跑不通,作为参考):varMongoClient=require('mongodb').MongoClient;MongoClient.connect('mongodb://localhost:27017/school',function(err,db){if(err)throwerr;varquery={};varcursor=db.collection('students').find(query);cursor.each(function(err,doc){if(err)throwerr;if(doc==null){returndb.close();}/*TODO*/vartarget1=doc.scores[2];vartarget2=doc.scores[3];if(target1
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 338 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信