Reputation: 193
Folks,
I have a MongoDB query that I would like to execute in my Python code:
db.authentication.aggregate(
{$group:{_id:{'cpf':"$person.cpf", 'data':"$created_at"}, 'chamadas':{$push:"$device_data.metaChamadas.duration"}}},
{$project:{
'cpf':"$_id.cpf",
'tamanho':{$size:{$arrayElemAt:["$chamadas",0]}}
}}
)
I´m using the Mongoengine since it supports raw PyMongo queries but I can´t figure out the conversion so far:
documents = list(Authentication.objects(__raw__={
PYMONGO QUERY HERE
}).limit(10))
Any thoughts?
Upvotes: 1
Views: 292
Reputation: 38922
All mongoengine.queryset.QuerySet
have an aggregate method that can be called with aggregation commands as arguments.
Translating your MongoDb query, you should have a query like so:
pipeline = (
{
'$group': {
'_id': {
'cpf': '$person.cpf',
'data': '$created_at'
},
'chamadas': {
'$push': '$device_data.metaChamadas.duration'
}
}
},
{
'$project': {
'cpf': '$_id.cpf',
'tamanho': {
'$size': {
'$arrayElemAt': ['$chamadas', 0]
}
}
}
}
)
data = Authentication.objects.aggregate(*pipeline)
print(list(data))
Upvotes: 3