【问题标题】:How to retrieve the _id of a document from a query MongoDB Python?如何从查询 MongoDB Python 中检索文档的 _id?
【发布时间】:2018-03-03 01:49:20
【问题描述】:

正在做一项作业,我正在编写逻辑。我想确定类型作业的最低分数,并根据该文档的“_id”删除它。这是我到目前为止所拥有的,但我无法提取_id。任何帮助都会很棒,我对此很陌生。

db.grades.find({'id': {'type': 'homework', 'score': {$min : 'score'}}})

【问题讨论】:

    标签: mongodb pymongo


    【解决方案1】:

    如果您只想找到最低分数,则升序排序并将输出限制为 1:

    db.grades.find({
      'type': 'homework'
    }).sort({'score': 1}).limit(1)
    

    或 使用聚合:

    db.grades.aggregate(
    [ 
    { 
       $match:{
          'type': 'homework'
    },
     {
        $group:{
          _id: null,
          minScore: { $min: "$score" }
        }
      },
     {
       $project:{
          "_id":1 
        //This query would return the id where score is minimum
       }
     }
    ]
    

    )

    您也可以参考以下内容获取更多帮助 How to find min value in mongodb

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 2021-09-09
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多