简体   繁体   English

如何使用 pymongo 查询嵌套字段

[英]How to query nested field with pymongo

I have a JSON file that contains order data, so each order has a field that looks like this (each line_items contains only 1 element in its list):我有一个包含订单数据的 JSON 文件,因此每个订单都有一个如下所示的字段(每个 line_items 列表中仅包含 1 个元素):

"line_items": [
            {
                "id": 1994,
                "name": "Hoodie - Blue, No",
                "product_id": 21,
                "variation_id": 39,
                "quantity": 5,
                "tax_class": "",
                "subtotal": "225.00",
                "subtotal_tax": "0.00",
                "total": "225.00",
                "total_tax": "0.00",
                "taxes": [],
                "meta_data": [
                    {
                        "id": 14439,
                        "key": "pa_color",
                        "value": "blue",
                        "display_key": "Color",
                        "display_value": "Blue"
                    },
                    {
                        "id": 14440,
                        "key": "logo",
                        "value": "No",
                        "display_key": "Logo",
                        "display_value": "No"
                    }
                ],
                "sku": "woo-hoodie-blue",
                "price": 45,
                "parent_name": "Hoodie"
            }
        ],

I'm trying to do pymongo search wth this code:我正在尝试使用以下代码进行 pymongo 搜索:

mongo_orders = list(col_orders.find({"line_items[0].product_id": 21}, {"_id": 0}))

But it always returns nothing.但它总是什么都不返回。 How do I do this one correctly?我该如何正确地做到这一点?

Try:尝试:

mongo_orders = list(col_orders.find({"line_items.0.product_id": 21}, {"_id": 0}))

This is an example of querying an array by index position这是通过索引 position 查询数组的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM