2013-02-09 58 views
2

我在MongoDB中以下schemea:删除特定属性类型的最小元素在MongoDB中

{ 
     "_id" : 100, 
     "name" : "John Doe", 
     "scores" : [ 
       { 
         "type" : "exam", 
         "score" : 334.45023 
       }, 
       { 
         "type" : "quiz", 
         "score" : 11.78273309957772 
       }, 
       { 
         "type" : "homework", 
         "score" : 6.676176060654615 
       }, 
       { 
         "type" : "homework", 
         "score" : 35.8740349954354 
       } 
     ] 
} 

我正在寻找一种方法,以消除用最少的得分作业。我找到了一个相关的答案here但是,这并没有什么帮助,因为我只需要找出最少分数的“家庭作业”并将其删除。

我正在使用MongoDB以及PyMongo驱动程序。

+7

这似乎是来自10ge的HW问题之一MongoDB课程。 – akotian 2013-02-12 13:33:36

+0

@akotian:作业截止日期已经结束。你可以提供一些帮助/提示。我不想要解决方案。 – hytriutucx 2013-02-12 17:26:26

+1

顺便说一下,该课程每年在MongoDB大学重新运行两次。 – tomaskazemekas 2015-10-27 12:11:13

回答

0

我不认为有可能使用本地mongodb命令。我认为这样做的最好方法是编写一个javascript函数来降低最低分数并在服务器上运行它;这将具有自动化的优势,以便在您从中删除列表时不会更新列表,从而保持一致。

下面是一些文档:http://docs.mongodb.org/manual/applications/server-side-javascript/

3

你需要添加match,如:

myresults = scores.aggregate([ { "$unwind": "$scores" }, { '$match': {'scores.type': "homework" } }, { "$group": { '_id':'$_id' , 'minitem': { '$min': "$scores.score" } } } ]) 

    for result in myresults['result']: 
     scores.update({ '_id': result['_id'] }, { '$pull': { 'scores': { 'score': result['minitem'] } } }) 
+0

我在mongo shell上试过这个,但得到语法错误 - 不知道问题出在哪里。下面是错误: 结果在myresults ['result']: scores.update({'_id':result ['_ id']},{'$ pull':{'scores':{'score' :result ['minitem']}}}) 2014-11-10T01:14:00.573 + 0530 SyntaxError:意外的标识符 – apratik 2014-11-09 20:03:30

0

我跟着第三个答案在这里。 Removing the minimum element of a particular attribute type in MongoDB

var MongoClient = require('mongodb').MongoClient; 

    MongoClient.connect('mongodb://localhost:27017/school', function(err, db) { 
     if(err) throw err; 

     var students = db.collection('students'); 
     cursor = students.aggregate(
    [{ "$unwind": "$scores" }, 
    { "$match": { "scores.type": "homework"}}, 
    { "$group": {'_id': '$_id', 'minitem': { 
        '$min': "$scores.score" 
       } 
      } 
     } 

    ]); 

    cursor.forEach(
    function(coll) { 
     students.update({ 
      '_id': coll._id 
     }, { 
      '$pull': { 
       'scores': { 
        'score': coll.minitem 
       } 
      } 
     }) 
    }); 
    }); 
1

我尝试使用本机mongodb命令,它的工作原理。 使用以下2个命令使其工作。

1) cursor = db.students.aggregate([{“$ unwind”:“$ scores”},{“$ match”:{“scores.type”:“homework”}},{“$ group':{'_id':'$ _id','minitem':{'$ min':“$ scores.score”}}}]),null

2) cursor.forEach(function ){db.students.update({'_ id':coll._id},{'$ pull':{'scores':{'score':coll.minitem}}})})

+0

第一个1)需要像这样修改:cursor = db.students.aggregate([{ “$ unwind”:“$ scores”},{“$ match”:{“scores.type”:“homework”}}, {“$ group”:{'_id':'$ _ id','minitem' :{'$ min':“$ scores.score”}}}]) – flame3 2016-04-05 07:51:26

1

这是使用Python的解决方案:

students = db.students 
cursor = students.find({}) 
for doc in cursor: 
     hw_scores = [] 
     for item in doc["scores"]: 
      if item["type"] == "homework": 
       hw_scores.append(item["score"]) 
     hw_scores.sort() 
     hw_min = hw_scores[0] 
     students.update({"_id": doc["_id"]}, 
         {"$pull":{"scores":{"score":hw_min}}})