1

Is it possible to have a contains and exists call in the same query in mongodb, specifically mongoengine for python?

The data I need to parse is stored in a mapfield where I need to go into the keys and identify if the key of interest exists and contains a specific word. here is an example of the data structure

{_id: "Final Fantasy"
"playtime": {"2024-01-01 1:00:00" : {"deaths":5, "levels_gained":10},
             "2024-01-03 4:00:00" : {"deaths":2, "levels_gained":5},
             "2024-01-07 3:00:00" : {"deaths":2, "levels_gained":5}}}

Currently to find very specific keys in the mapfield, I perform the following

entries: List[GameDoc] = []
dt_str = "2024-01-03 4:00:00"
query = Q(**{f'playtime__{dt_str}__exists': True})
entries.extend(GameDoc.objects(query))

but when I try to include "contains" in the query, it doesn't find the documents. as an example, I want to find all documents that contain the date "2024-01" regardless of the other items in the key.

query = Q(**{f'playtime__{"2024-01"}__exists': True})
query = Q(**{f'playtime__{"2024-01"}__contains': True})

0

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.