mongoDB聚集结构导致某些值返回,因为

发布于 2025-02-09 12:24:34 字数 1701 浏览 1 评论 0原文

我使用MongoDB聚合管道编写了一个简单的功能。试图运行它时会发生问题。由于某种原因,min_price和max_price都返回,而不是实际值。单个文档的格式为这样,

  {
    "_id": {
      "$oid": "62afc6584c7476a02643b61f"
    },
    "item_name": "Titanic Super Heavy Leggings §6✪§6✪§6✪§6✪§6✪",
    "auction_id": "516d82192fef4c3aa178b134e75fc0e1",
    "seller": "dae37eba5f594922bc8d79b9a78f8543",
    "seller_profile": "585d9e30f644496cafa0b36b555ef788",
    "buyer": "7a3699ca1eef40929c714362ab35915a",
    "timestamp": {
      "$numberLong": "1655686642260"
    },
    "price": {
      "$numberInt": "1000000"
    },
    "bin": true
  }

以下是用于计算最小和最大的功能,并按日期对它们进行排序。

def min_max_volume(item_name):
    db = client.test
    mycol = db["ended"]
    pipeline = [
        {"$match": {"item_name": item_name}},
        {"$project": {
            'datetime': { '$dateToString': { 'format': "%Y-%m-%d", 'date': { "$toDate": "$timestamp"}}},
        }},
        {"$group": {
            '_id': '$datetime',
            'min_price': {'$min': '$price'},
            'max_price': {'$max': '$price'},
        }}
    ]
    results = mycol.aggregate(pipeline)
    for i in results:
        print(i)

预期的输出应该像这样,

{'_id': '2022-06-20', 'min_price': 345839475, 'max_price': 48534875}
{'_id': '2022-06-21', 'min_price': 456567, 'max_price': 348573945}
{'_id': '2022-06-22', 'min_price': 6486956, 'max_price': 12938291}

而是在min_price和max_price字段中不包含值。

{'_id': '2022-06-20', 'min_price': None, 'max_price': None}
{'_id': '2022-06-21', 'min_price': None, 'max_price': None}
{'_id': '2022-06-22', 'min_price': None, 'max_price': None}

I have written a simple function using mongoDB aggregation pipelines. The issue occurs when attempting to run it. For some reason min_price and max_price both return as none instead of a real value. The individual documents are formatted as so,

  {
    "_id": {
      "$oid": "62afc6584c7476a02643b61f"
    },
    "item_name": "Titanic Super Heavy Leggings §6✪§6✪§6✪§6✪§6✪",
    "auction_id": "516d82192fef4c3aa178b134e75fc0e1",
    "seller": "dae37eba5f594922bc8d79b9a78f8543",
    "seller_profile": "585d9e30f644496cafa0b36b555ef788",
    "buyer": "7a3699ca1eef40929c714362ab35915a",
    "timestamp": {
      "$numberLong": "1655686642260"
    },
    "price": {
      "$numberInt": "1000000"
    },
    "bin": true
  }

Below is the function used to calculate min and max and sorting them by date.

def min_max_volume(item_name):
    db = client.test
    mycol = db["ended"]
    pipeline = [
        {"$match": {"item_name": item_name}},
        {"$project": {
            'datetime': { '$dateToString': { 'format': "%Y-%m-%d", 'date': { "$toDate": "$timestamp"}}},
        }},
        {"$group": {
            '_id': '$datetime',
            'min_price': {'$min': '$price'},
            'max_price': {'$max': '$price'},
        }}
    ]
    results = mycol.aggregate(pipeline)
    for i in results:
        print(i)

The expected output should look like this,

{'_id': '2022-06-20', 'min_price': 345839475, 'max_price': 48534875}
{'_id': '2022-06-21', 'min_price': 456567, 'max_price': 348573945}
{'_id': '2022-06-22', 'min_price': 6486956, 'max_price': 12938291}

but instead does not contain values in the min_price and max_price fields.

{'_id': '2022-06-20', 'min_price': None, 'max_price': None}
{'_id': '2022-06-21', 'min_price': None, 'max_price': None}
{'_id': '2022-06-22', 'min_price': None, 'max_price': None}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2025-02-16 12:24:34

$项目阶段缺少价格字段

price field is missing in $project stage

忘你却要生生世世 2025-02-16 12:24:34

管道第一阶段的输出将是第二阶段的输入。同样,它继续。在项目的第二阶段,您错过了增加价格。然后,价格不在集体阶段进行查询。

 pipeline = [
            {"$match": {"item_name": item_name}},
            {"$project": {
                'datetime': { '$dateToString': { 'format': "%Y-%m-%d", 'date': { "$toDate": "$timestamp"}}}, 'price':1
            }},
            {"$group": {
                '_id': '$datetime',
                'min_price': {'$min': '$price'},
                'max_price': {'$max': '$price'},
            }}
        ]

The output of first stage of pipeline will be the input of second stage. Likewise it continues. In the second stage i.e. in project, you missed to add price. Then, price is not in the group stage to query.

 pipeline = [
            {"$match": {"item_name": item_name}},
            {"$project": {
                'datetime': { '$dateToString': { 'format': "%Y-%m-%d", 'date': { "$toDate": "$timestamp"}}}, 'price':1
            }},
            {"$group": {
                '_id': '$datetime',
                'min_price': {'$min': '$price'},
                'max_price': {'$max': '$price'},
            }}
        ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文