2

Using PyMongo, how would one find/search for the documents where the nested array json object matches a given string.

Given the following 2 Product JSON documents in a MongoDB collection..

[{
    "_id" : ObjectId("5be1a1b2aa21bb3ceac339b0"),
    "id" : "1",
    "prod_attr" : [ 
        {
            "name" : "Branded X 1 Sneaker"
        },  
        {
            "hierarchy" : {
                "dept" : "10",
                "class" : "101",
                "subclass" : "1011"
            }
        }
    ]
},
{
    "_id" : ObjectId("7be1a1b2aa21bb3ceac339xx"),
    "id" : "2",
    "prod_attr" : [ 
        {
            "name" : "Branded Y 2 Sneaker"
        },  
        {
            "hierarchy" : {
                "dept" : "10",
                "class" : "101",
                "subclass" : "2022"
            }
        }
    ]
}
]

I would like to 1. return all documents where prod_att.hierarchy.subclass = "2022" 2. return all documents where prod_attr.name contains "Sneaker"

I appreciate the JSON could be structured differently, unfortunately that is not within my control to change.

1 Answer 1

2

1. Return all documents where prod_attr.hierarchy.subclass = "2022"

Based on the Query an Array of Embedded Documents documentation of MongoDB you can use dot notation concatenating the name of the array field (prod_attr), with a dot (.) and the name of the field in the nested document (hierarchy.subclass):

collection.find({"prod_attr.hierarchy.subclass": "2022"})

2. Return all documents where prod_attr.name contains "Sneaker"

As before, you can use the dot notation to query a field of a nested element inside an array. To perform the "contains" query you have to use the $regex operator:

collection.find({"prod_attr.name": {"$regex": "Sneaker"}})

Another option is to use the MongoDB Aggregation framework:

collection.aggregate([
    {"$unwind": "$prod_attr"},
    {"$match": {"prod_attr.hierarchy.subclass": "2022"}}
])

the $unwind operator creates a new object for each object inside the prod_attr array, so you will have only nested documents and no array (check the documentation for details).
The next step is the $match operator that actually perform a query on the nested object.

This is a simple example but playing with the Aggregators Operators you have a lot of flexibility.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.