LogicGoInfotechSpaces commited on
Commit
416edc3
·
1 Parent(s): c90ea81

Ensure ai_edit_last_date is stored as string (DD/MM/YYYY format) and add MongoDB queries documentation

Browse files
Files changed (2) hide show
  1. app.py +4 -3
  2. mongodb_queries.md +271 -0
app.py CHANGED
@@ -290,8 +290,8 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
290
  return False
291
 
292
  now = datetime.utcnow()
293
- # Format date as DD/MM/YYYY (e.g., 12/12/2025)
294
- date_formatted = now.strftime("%d/%m/%Y")
295
 
296
  # Check if document with userId exists
297
  existing_doc = collection.find_one({"userId": user_object_id})
@@ -482,7 +482,8 @@ def save_media_click(user_id: Optional[Union[int, str]], category_id: str):
482
  if existing:
483
  # Document exists, try to add category
484
  now_retry = datetime.utcnow()
485
- date_formatted_retry = now_retry.strftime("%d/%m/%Y")
 
486
  result = collection.update_one(
487
  {"userId": user_object_id},
488
  {
 
290
  return False
291
 
292
  now = datetime.utcnow()
293
+ # Format date as DD/MM/YYYY string (e.g., "13/12/2025") - stored as string, not Date object
294
+ date_formatted = str(now.strftime("%d/%m/%Y"))
295
 
296
  # Check if document with userId exists
297
  existing_doc = collection.find_one({"userId": user_object_id})
 
482
  if existing:
483
  # Document exists, try to add category
484
  now_retry = datetime.utcnow()
485
+ # Format date as DD/MM/YYYY string (e.g., "13/12/2025") - stored as string, not Date object
486
+ date_formatted_retry = str(now_retry.strftime("%d/%m/%Y"))
487
  result = collection.update_one(
488
  {"userId": user_object_id},
489
  {
mongodb_queries.md ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # MongoDB Queries for media_clicks Collection
2
+
3
+ ## Database: admin (or your admin database name)
4
+ ## Collection: media_clicks
5
+
6
+ ---
7
+
8
+ ## 1. Find documents with ai_edit_last_date field
9
+
10
+ ### Find all documents that have ai_edit_last_date
11
+ ```javascript
12
+ db.media_clicks.find({ ai_edit_last_date: { $exists: true } })
13
+ ```
14
+
15
+ ### Find documents with specific ai_edit_last_date
16
+ ```javascript
17
+ // Find by exact date (format: DD/MM/YYYY)
18
+ db.media_clicks.find({ ai_edit_last_date: "12/12/2025" })
19
+ ```
20
+
21
+ ### Find documents with ai_edit_last_date in date range
22
+ ```javascript
23
+ // Find documents with dates in December 2025
24
+ db.media_clicks.find({
25
+ ai_edit_last_date: {
26
+ $regex: /^\\d{2}\/12\/2025$/
27
+ }
28
+ })
29
+ ```
30
+
31
+ ### Find documents with ai_edit_last_date NOT null/empty
32
+ ```javascript
33
+ db.media_clicks.find({
34
+ ai_edit_last_date: {
35
+ $exists: true,
36
+ $ne: null,
37
+ $ne: ""
38
+ }
39
+ })
40
+ ```
41
+
42
+ ---
43
+
44
+ ## 2. Check if ai_edit_complete field exists
45
+
46
+ ### Find documents that HAVE ai_edit_complete field
47
+ ```javascript
48
+ db.media_clicks.find({ ai_edit_complete: { $exists: true } })
49
+ ```
50
+
51
+ ### Find documents that DO NOT have ai_edit_complete field (old data)
52
+ ```javascript
53
+ db.media_clicks.find({ ai_edit_complete: { $exists: false } })
54
+ ```
55
+
56
+ ### Find documents where ai_edit_complete is 0 (new users who haven't used model yet)
57
+ ```javascript
58
+ db.media_clicks.find({ ai_edit_complete: 0 })
59
+ ```
60
+
61
+ ### Find documents where ai_edit_complete is greater than 0 (users who have used models)
62
+ ```javascript
63
+ db.media_clicks.find({ ai_edit_complete: { $gt: 0 } })
64
+ ```
65
+
66
+ ---
67
+
68
+ ## 3. Combined Queries
69
+
70
+ ### Find documents with both fields
71
+ ```javascript
72
+ db.media_clicks.find({
73
+ ai_edit_complete: { $exists: true },
74
+ ai_edit_last_date: { $exists: true }
75
+ })
76
+ ```
77
+
78
+ ### Find documents missing either field (old data)
79
+ ```javascript
80
+ db.media_clicks.find({
81
+ $or: [
82
+ { ai_edit_complete: { $exists: false } },
83
+ { ai_edit_last_date: { $exists: false } }
84
+ ]
85
+ })
86
+ ```
87
+
88
+ ### Find users who have used models (ai_edit_complete > 0) with their last date
89
+ ```javascript
90
+ db.media_clicks.find({
91
+ ai_edit_complete: { $gt: 0 },
92
+ ai_edit_last_date: { $exists: true }
93
+ })
94
+ ```
95
+
96
+ ---
97
+
98
+ ## 4. Count Queries
99
+
100
+ ### Count documents with ai_edit_last_date
101
+ ```javascript
102
+ db.media_clicks.countDocuments({ ai_edit_last_date: { $exists: true } })
103
+ ```
104
+
105
+ ### Count documents with ai_edit_complete field
106
+ ```javascript
107
+ db.media_clicks.countDocuments({ ai_edit_complete: { $exists: true } })
108
+ ```
109
+
110
+ ### Count documents without ai_edit_complete (old data)
111
+ ```javascript
112
+ db.media_clicks.countDocuments({ ai_edit_complete: { $exists: false } })
113
+ ```
114
+
115
+ ### Count users who have used models (ai_edit_complete > 0)
116
+ ```javascript
117
+ db.media_clicks.countDocuments({ ai_edit_complete: { $gt: 0 } })
118
+ ```
119
+
120
+ ---
121
+
122
+ ## 5. Aggregation Queries
123
+
124
+ ### Get statistics for ai_edit_complete
125
+ ```javascript
126
+ db.media_clicks.aggregate([
127
+ {
128
+ $group: {
129
+ _id: null,
130
+ total_users: { $sum: 1 },
131
+ users_with_field: {
132
+ $sum: { $cond: [{ $ifNull: ["$ai_edit_complete", false] }, 1, 0] }
133
+ },
134
+ users_without_field: {
135
+ $sum: { $cond: [{ $ifNull: ["$ai_edit_complete", false] }, 0, 1] }
136
+ },
137
+ avg_ai_edit_complete: { $avg: "$ai_edit_complete" },
138
+ max_ai_edit_complete: { $max: "$ai_edit_complete" },
139
+ min_ai_edit_complete: { $min: "$ai_edit_complete" }
140
+ }
141
+ }
142
+ ])
143
+ ```
144
+
145
+ ### Get users by ai_edit_complete range
146
+ ```javascript
147
+ db.media_clicks.aggregate([
148
+ {
149
+ $group: {
150
+ _id: {
151
+ $switch: {
152
+ branches: [
153
+ { case: { $eq: ["$ai_edit_complete", null] }, then: "No field (old data)" },
154
+ { case: { $eq: ["$ai_edit_complete", 0] }, then: "0 (new, not used)" },
155
+ { case: { $lte: ["$ai_edit_complete", 5] }, then: "1-5 uses" },
156
+ { case: { $lte: ["$ai_edit_complete", 10] }, then: "6-10 uses" },
157
+ { case: { $gt: ["$ai_edit_complete", 10] }, then: "10+ uses" }
158
+ ],
159
+ default: "Unknown"
160
+ }
161
+ },
162
+ count: { $sum: 1 }
163
+ }
164
+ },
165
+ { $sort: { _id: 1 } }
166
+ ])
167
+ ```
168
+
169
+ ### Get latest ai_edit_last_date for each user
170
+ ```javascript
171
+ db.media_clicks.find(
172
+ { ai_edit_last_date: { $exists: true } },
173
+ {
174
+ userId: 1,
175
+ ai_edit_last_date: 1,
176
+ ai_edit_complete: 1
177
+ }
178
+ ).sort({ ai_edit_last_date: -1 })
179
+ ```
180
+
181
+ ---
182
+
183
+ ## 6. Update Queries (if needed to fix old data)
184
+
185
+ ### Add default ai_edit_complete = 0 to documents missing it
186
+ ```javascript
187
+ db.media_clicks.updateMany(
188
+ { ai_edit_complete: { $exists: false } },
189
+ { $set: { ai_edit_complete: 0 } }
190
+ )
191
+ ```
192
+
193
+ ### Set ai_edit_last_date based on updatedAt for old documents
194
+ ```javascript
195
+ // Note: This converts updatedAt to DD/MM/YYYY format
196
+ // Run this carefully as it modifies data
197
+ db.media_clicks.find({
198
+ ai_edit_last_date: { $exists: false },
199
+ updatedAt: { $exists: true }
200
+ }).forEach(function(doc) {
201
+ var date = doc.updatedAt;
202
+ var formatted = date.getDate().toString().padStart(2, '0') + '/' +
203
+ (date.getMonth() + 1).toString().padStart(2, '0') + '/' +
204
+ date.getFullYear();
205
+ db.media_clicks.updateOne(
206
+ { _id: doc._id },
207
+ { $set: { ai_edit_last_date: formatted } }
208
+ );
209
+ })
210
+ ```
211
+
212
+ ---
213
+
214
+ ## 7. Find by userId (ObjectId)
215
+
216
+ ### Find specific user by userId
217
+ ```javascript
218
+ // Replace with actual ObjectId
219
+ db.media_clicks.find({
220
+ userId: ObjectId("693652b3f8683fd35b75448a")
221
+ })
222
+ ```
223
+
224
+ ### Find user and check if fields exist
225
+ ```javascript
226
+ db.media_clicks.findOne(
227
+ { userId: ObjectId("693652b3f8683fd35b75448a") },
228
+ {
229
+ userId: 1,
230
+ ai_edit_complete: 1,
231
+ ai_edit_last_date: 1,
232
+ categories: 1,
233
+ updatedAt: 1
234
+ }
235
+ )
236
+ ```
237
+
238
+ ---
239
+
240
+ ## 8. Find by Category
241
+
242
+ ### Find users who used specific category with ai_edit fields
243
+ ```javascript
244
+ db.media_clicks.find({
245
+ "categories.categoryId": ObjectId("69368d62b95a6c2a75920505"),
246
+ ai_edit_complete: { $exists: true }
247
+ })
248
+ ```
249
+
250
+ ---
251
+
252
+ ## Quick Test Queries
253
+
254
+ ### Check if field exists in sample document
255
+ ```javascript
256
+ db.media_clicks.findOne({}, {
257
+ userId: 1,
258
+ ai_edit_complete: 1,
259
+ ai_edit_last_date: 1
260
+ })
261
+ ```
262
+
263
+ ### Get sample of documents with and without fields
264
+ ```javascript
265
+ // With fields
266
+ db.media_clicks.find({ ai_edit_complete: { $exists: true } }).limit(5)
267
+
268
+ // Without fields (old data)
269
+ db.media_clicks.find({ ai_edit_complete: { $exists: false } }).limit(5)
270
+ ```
271
+