如果我有client
个和task
个Collection
task
{
fields: {
client: [
{
uuid: 'b5050bf2-ca75-4b0a-920c-628ca45f929a',
}
],
uuid: '35c0541d-dd39-420b-9f74-027a9a268f88',
}
}
client
个
{
updated_at: ISODate('2024-03-18T12:07:17.313Z'),
fields: {
uuid: 'b5050bf2-ca75-4b0a-920c-628ca45f929a',
}
}
如何列出所有任务与他们的最新客户?我有代码,但它不返回客户端数据在related_client
field
list_of_conditions = [
{'$sort': {'updated_at': pymongo.DESCENDING}},
{'$group': {'_id': '$fields.uuid', 'doc': {'$first': '$$ROOT'}}},
{'$replaceRoot': {'newRoot': '$doc'}},
{'$match':
{
'fields.uuid': {'$in': list(tasks_uuids)},
},
},
{'$lookup':
{
'from': 'client',
'let': {'client_uuid': '$fields.uuid'},
'pipeline': [
{'$match': {'$expr': {'$eq': ['$fields.client.uuid', '$$client_uuid']}}},
{'$sort': {'updated_at': -1}},
{'$limit': 1}
],
'as': 'related_client',
},
},
]
collection = await mongo_uow.task_actions.get_db_collection() # task collection
collection_objects = collection.aggregate(list_of_conditions)