Skip to content

Fix nested array element queries - support querying array elements with dot notation#51

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-e158f087-eb28-4ff6-8121-3069c2aea70e
Draft

Fix nested array element queries - support querying array elements with dot notation#51
Copilot wants to merge 2 commits intomasterfrom
copilot/fix-e158f087-eb28-4ff6-8121-3069c2aea70e

Conversation

Copy link

Copilot AI commented Jul 27, 2025

Fixes issue #44 where querying nested array elements using dot notation (e.g., results.product) was not working correctly.

Problem

When attempting to query documents with arrays of objects using dot notation, Mongita would return no results even when matching documents existed:

# This collection data:
collection_data = [
    {
        "_id": 1,
        "results": [
            {"product": "abc", "score": 10},
            {"product": "xyz", "score": 5}
        ]
    },
    # ... more documents with xyz products
]

# This query would return [] instead of matching documents:
list(db.collection.find({"results.product": "xyz"}))

Root Cause

The _get_item_from_doc function in collection.py was not designed to handle array element queries. When encountering a path like results.product where results is an array of objects, it would try to access array["product"] which fails since arrays don't have named properties.

Solution

  1. Enhanced _get_item_from_doc function: Modified to detect when a path component is not a numeric array index and the current item is an array. In such cases, it now searches through each array element (if it's an object) and recursively applies the remaining path, returning all matching values as a list.

  2. Updated _doc_matches_agg function: Enhanced all query operators ($eq, $ne, $lt, $lte, $gt, $gte, $in, $nin) to properly handle list values returned from nested array queries. For comparison operators, it now checks if ANY element in the array matches the condition (following MongoDB semantics).

Changes Made

  • Modified _get_item_from_doc to handle array element queries with dot notation
  • Enhanced _doc_matches_agg to support all query operators with array element results
  • Added comprehensive test suite test_nested_array_queries covering:
    • Basic equality queries on nested array elements
    • All query operators with array elements
    • Combined conditions
    • Edge cases (empty arrays, mixed content, deep nesting)

Verification

The fix now correctly handles the original issue example:

# Now works correctly and returns 3 matching documents
list(db.collection.find({"results.product": "xyz"}))

# Also supports all query operators:
list(db.collection.find({"results.score": {"$gt": 8}}))
list(db.collection.find({"results.product": {"$in": ["abc", "def"]}}))

All existing tests continue to pass, ensuring backward compatibility is maintained.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…th dot notation

Co-authored-by: scottrogowski <466747+scottrogowski@users.noreply.github.com>
Copilot AI changed the title [WIP] @scottrogowski/mongita/issues/44 Fix nested array element queries - support querying array elements with dot notation Jul 27, 2025
Copilot AI requested a review from scottrogowski July 27, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants