Commit
·
bd8f603
1
Parent(s):
88106f3
Add multiple nested query patterns to check_user_has_category_data for better MongoDB compatibility
Browse files- app/smart_recommendation.py +36 -11
app/smart_recommendation.py
CHANGED
|
@@ -173,6 +173,7 @@ class SmartBudgetRecommender:
|
|
| 173 |
|
| 174 |
# FIRST: Check nested structure (headCategories[].categories[].category) - most common case
|
| 175 |
# Budgets have structure: headCategories[].categories[].category (ObjectId)
|
|
|
|
| 176 |
print(f"🔍 Checking nested structure: headCategories[].categories[].category for category_id {category_id}")
|
| 177 |
try:
|
| 178 |
try:
|
|
@@ -180,9 +181,11 @@ class SmartBudgetRecommender:
|
|
| 180 |
except (ValueError, TypeError):
|
| 181 |
category_objid = category_id
|
| 182 |
|
| 183 |
-
# Try to
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
"$and": [
|
| 187 |
{"$or": user_conditions},
|
| 188 |
{
|
|
@@ -196,14 +199,36 @@ class SmartBudgetRecommender:
|
|
| 196 |
]
|
| 197 |
}
|
| 198 |
]
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
except Exception as e:
|
| 208 |
print(f"⚠️ Error checking nested categories: {e}")
|
| 209 |
import traceback
|
|
|
|
| 173 |
|
| 174 |
# FIRST: Check nested structure (headCategories[].categories[].category) - most common case
|
| 175 |
# Budgets have structure: headCategories[].categories[].category (ObjectId)
|
| 176 |
+
# User ID is stored in createdBy field
|
| 177 |
print(f"🔍 Checking nested structure: headCategories[].categories[].category for category_id {category_id}")
|
| 178 |
try:
|
| 179 |
try:
|
|
|
|
| 181 |
except (ValueError, TypeError):
|
| 182 |
category_objid = category_id
|
| 183 |
|
| 184 |
+
# Try multiple nested query patterns to ensure we catch all cases
|
| 185 |
+
nested_queries = []
|
| 186 |
+
|
| 187 |
+
# Pattern 1: Using $elemMatch for nested arrays (most accurate)
|
| 188 |
+
nested_queries.append({
|
| 189 |
"$and": [
|
| 190 |
{"$or": user_conditions},
|
| 191 |
{
|
|
|
|
| 199 |
]
|
| 200 |
}
|
| 201 |
]
|
| 202 |
+
})
|
| 203 |
+
|
| 204 |
+
# Pattern 2: Using dot notation (simpler, might work for some MongoDB versions)
|
| 205 |
+
nested_queries.append({
|
| 206 |
+
"$and": [
|
| 207 |
+
{"$or": user_conditions},
|
| 208 |
+
{
|
| 209 |
+
"$or": [
|
| 210 |
+
{"headCategories.categories.category": category_objid},
|
| 211 |
+
{"headCategories.categories.category": category_id},
|
| 212 |
+
{"headCategories.headCategory": category_objid},
|
| 213 |
+
{"headCategories.headCategory": category_id},
|
| 214 |
+
]
|
| 215 |
+
}
|
| 216 |
+
]
|
| 217 |
+
})
|
| 218 |
+
|
| 219 |
+
# Try each nested query pattern
|
| 220 |
+
for i, nested_query in enumerate(nested_queries, 1):
|
| 221 |
+
try:
|
| 222 |
+
nested_count = self.db.budgets.count_documents(nested_query)
|
| 223 |
+
if nested_count > 0:
|
| 224 |
+
print(f"✅ Found {nested_count} budget(s) with nested category structure for user {user_id} with category_id {category_id}")
|
| 225 |
+
print(f" Nested query pattern {i} matched: headCategories[].categories[].category = {category_id}")
|
| 226 |
+
return True
|
| 227 |
+
except Exception as e:
|
| 228 |
+
print(f"⚠️ Error with nested query pattern {i}: {e}")
|
| 229 |
+
continue
|
| 230 |
+
|
| 231 |
+
print(f"🔍 Nested query found 0 budgets for category_id {category_id}")
|
| 232 |
except Exception as e:
|
| 233 |
print(f"⚠️ Error checking nested categories: {e}")
|
| 234 |
import traceback
|