MongoDB,Flask,使用$ unwind查询进行聚合 [英] MongoDB, Flask, aggregate with $unwind query

查看:107
本文介绍了MongoDB,Flask,使用$ unwind查询进行聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用pymongo连接到MongoDB的Flask应用程序.当前,数据库包含有关以以下格式运行的数据:

I have a Flask application connected to MongoDB using pymongo. Currently the database contains data about runs in this format:

{
    "_id": {
        "$oid": "5df031cec687bf2b4c4349b9"
    },
    "run_number": "1",
    "frames": [{
        "frame_number": 1,
        "data": {
            "brake": "0.1",
            "steer": "0.4",
            "throttle": "0.6"
        }
    }, {
        "frame_number": 2,
        "data": {
            "brake": "0.2",
            "steer": "0.8",
            "throttle": "0.6"
        }
    }, {
        "frame_number": 3,
        "data": {
            "brake": "0.6",
            "steer": "0.2",
            "throttle": "0.1"
        }
    }]
}

我能够使用此端点检索特定运行的所有数据:

I am able to retrieve all the data of a specific run with this endpoint:

@app.route('/pitcrew-purple/api/v1/<run_number>/')
def get_run(run_number):
    if run_number:
        data = []
        for i in mongodb.pitcrewdb.find({"run_number": run_number}):
            i.pop('_id')
            data.append(i)
        if not data:
            return "No data was found for run number {}".format(run_number), 400
        return jsonify(data), 200
    return "No run_number was given", 400

问题

我正在使用Flask API端点,该端点将从frame_number大于或等于给定帧号的特定run_number返回所有数据.

Problem

I am working on a Flask API endpoint that will return all data from a certain run_number where the frame_number is greater than or equal to the given frame number.

因此,当输入run_number = 1frame_number = 2作为输入时,应检索运行编号1中来自帧编号> = 2的所有数据.

So when run_number = 1 and frame_number = 2 are given as input, all data from frame numbers >= 2 from run number 1 should be retrieved.

我是MongoDB的新手,已经阅读了文档,但是无法从数据库中获取我想要的数据.

I am new to MongoDB and have read the docs but am unable to get the data I want from the database.

我尝试通过此端点检索所需的数据

I tried to retrieve the desired data with this endpoint

@app.route('/pitcrew-purple/api/v1/<run_number>/<start_frame_number>/')
def get_rundata_from_start_frame(run_number, start_frame_number):
    if run_number and start_frame_number:
        data = []
        query = mongodb.pitcrewdb.find(
            {"run_number": run_number, "frames.frame_number": {"$gte": start_frame_number}}
        )
        for i in query:
            i.pop("frames.frame_number")
            data.append(i)
        if not data:
            return "The specified data starting from frame number: {} was not found".format(start_frame_number), 400
        return jsonify(data), 200
    return "No parameters were given", 400

我还尝试用i.pop("_id")替换i.pop("frames.frame_number"),但是这两个结果均导致找不到从帧号开始的指定数据:2.".

I also tried to replace i.pop("frames.frame_number") with i.pop("_id") but both of these result in "The specified data starting from frame number: 2 was not found".

为了从我的MongoDB中检索所需的数据,我应该改变什么?

What should I change in order to retrieve the desired data from my MongoDB?

"run_number": "1",
    "frames": [{  
      {
        "frame_number": 2,
        "data": {
            "brake": "0.2",
            "steer": "0.8",
            "throttle": "0.6"
        }
    }, {
        "frame_number": 3,
        "data": {
            "brake": "0.6",
            "steer": "0.2",
            "throttle": "0.1"
        }
    }]

推荐答案

您可以通过使用$unwind然后使用$group$push来实现这一点,就像这样:

You can achieve that by using an $unwind and then a $group with $push, like this:

db.pitcrewdb.aggregate([
  {
    "$match": {
      "run_number": "1",
      "frames.frame_number": {
        "$gte": 2
      }
    }
  },
  {
    "$project": {
      "_id": 0
    }
  },
  {
    "$unwind": "$frames"
  },
  {
    "$match": {
      "frames.frame_number": {
        "$gte": 2
      }
    }
  },
  {
    "$group": {
      "_id": "$run_number",
      "frames": {
        "$push": "$frames"
      }
    }
  }
])

$unwind将破坏您的数组,然后过滤结果,并在$group之后再次输入结果

the $unwind will destruct your array and then you filter the results and after $group them again

或者您也可以使用$filter

db.pitcrewdb.aggregate([
  {
    "$match": {
      "run_number": "1"
    }
  },
  {
    "$project": {
      "_id": 0,
      "frames": {
        $filter: {
          input: "$frames",
          as: "frame",
          cond: {
            $gte: [
              "$$frame.frame_number",
              2
            ]
          }
        }
      }
    }
  }
])

这篇关于MongoDB,Flask,使用$ unwind查询进行聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆