描述:格式如下所示,其中每个对象有_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
添加回答
举报
0/150
提交
取消