My documents are like this below:
{
"_id": ObjectId("63bd85a644552d73f36c366e"),
"ts": datetime.datetime(2023, 1, 10, 15, 35, 2, 374000),
"dt": datetime.datetime(2023, 1, 10, 15, 34, 10),
"aff": 2,
"src": 2,
"st": 2
}
I started using $searchMeta
faceting for a better and way faster approach.
So, I converted my aggregation pipeline from this approach:
results = my_collection.aggregate(
[
{
"$match": {
"ts": {"$gte": datetime.now() - timedelta(hours=24)},
"st": 2
}
},
{
"$group": {
"_id": {"aff": "$aff", "src": "$src"},
"count": {"$sum": 1},
"last_message_datetime": {"$max": "$dt"}
}
},
{"$sort": {"_id.src": -1, "_id.aff": -1}}
]
)
into this below:
results = my_collection.aggregate([
{
"$searchMeta": {
"index": "MsgAtlasIndex",
"count": {"type": "total"},
"facet": {
"operator": {
"compound": {
"must": [
{
"range": {
"path": "ts",
"gte": datetime.now() - timedelta(hours=24)
}
},
{"equals": {"path": "st", "value": 2}},
]
}
},
"facets": {
"src": {
"type": "number",
"path": "src",
"boundaries": [1, 2, 3, 4, 5, 6, 7, 10, 11]
},
"aff": {
"type": "number",
"path": "aff",
"boundaries": [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 17, 18, 19, 20, 21]
},
},
},
}
},
{
"$match": {
"$or": [
{"facet.aff.buckets.count": {"$gt": 0}},
{"facet.src.buckets.count": {"$gt": 0}}
]
}
},
{
"$project": {
"_id": 0,
"count": 1,
"facet.aff.buckets": {
"$filter": {
"input": "$facet.aff.buckets",
"as": "bucket",
"cond": {"$gt": ["$$bucket.count", 0]}
}
},
"facet.src.buckets": {
"$filter": {
"input": "$facet.src.buckets",
"as": "bucket",
"cond": {"$gt": ["$$bucket.count", 0]}
}
}
}
}
])
I suffered with how to apply this line below in searchMeta:
"last_message_datetime": {"$max": "$dt"}
It is to get the maximum date of the counted messages:
(the date of the last message of every group) using the field dt
(date).
Any help, please?