【问题标题】:Removing the minimum element of a particular attribute type in MongoDB删除 MongoDB 中特定属性类型的最小元素
【发布时间】:2013-01-25 03:33:45
【问题描述】:

我在 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 驱动程序。

【问题讨论】:

  • 这似乎是 10gen MongoDB 课程中的硬件问题之一。
  • @akotian:作业截止日期已经结束。您可以提供一些帮助/提示。我不想要解决方案。
  • 顺便说一下,该课程每年在 MongoDB 大学重播两次。

标签: mongodb pymongo


【解决方案1】:

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

这里有一些文档:http://docs.mongodb.org/manual/applications/server-side-javascript/

【讨论】:

    【解决方案2】:

    你需要添加matchlike:

        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'] } } } )
    

    【讨论】:

    • 我在 mongo shell 上试​​过这个,但出现语法错误 - 不确定问题出在哪里。以下是错误:对于结果 myresults['result']: score.update( { '_id': result['_id'] }, {'$pull': { 'scores': { 'score': result[ 'miniitem'] } } } ) 2014-11-10T01:14:00.573+0530 SyntaxError: Unexpected identifier
    【解决方案3】:

    我在这里遵循了第三个答案。 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
                    }
                }
            })
        });
        });
    

    【讨论】:

      【解决方案4】:

      我尝试使用本机 mongodb 命令并且它有效。 使用以下 2 个命令使其工作。

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

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

      【讨论】:

      • 第一个1)需要这样修改: cursor = db.students.aggregate( [ { "$unwind": "$scores" }, { "$match": { "scores. type": "homework"}}, {"$group": { '_id':'$_id' , 'miniitem': {'$min': "$scores.score" } } } ])
      【解决方案5】:

      这是一个使用 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}}})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-03
        • 2019-11-05
        • 2018-04-19
        • 1970-01-01
        • 2020-10-08
        • 1970-01-01
        • 1970-01-01
        • 2020-10-24
        相关资源
        最近更新 更多