简体   繁体   中英

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):

"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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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