Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
How to cleanly access deeply embeded fields in pymongo?

My documents are stored in the format sysstat.host.statistics.timestamp[].cpu-load-all.cpu[].usr, where timestamp is a 30 element array, and cpu is a 1-64 element array.

enter image description here

If I grab the timestamp field,

timestampCursor = HOST_USAGE.find(
   {'sysstat.host.nodename': host}, 
   {'sysstat.host.statistics.timestamp': 1})

How can I then access sysstat.host.statistics.timestamp[*].cpu-load-all.cpu[0].usr, cleanly? Do I have to access each field by indexing each array, and so multiple iterations over each array-field?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Yes, you have to access each field by indexing each array, and so multiple iterations over each array-field.

for doc in timestampCursor:
    sysstat = doc['sysstat']
    for ts in sysstat['host']['statistics']['timestamp']:
        for cpu in ts['cpu-load-all']['cpu']:
            usr = cpu['usr']
            # Now, sum or average the 'usr' values, or whatever
            # you intend to do.

Alternatively, to aggregate the data server-side, you can use $unwind with $sum or $average or some other grouping operation with the MongoDB Aggregation Framework.

HOST_USAGE.aggregate([{
    '$match': {'sysstat.host.nodename': 1}
}, {
    # Rename the field for brevity.
    '$project': {'ts': '$sysstat.host.statistics.timestamp'}
}, {
    '$unwind': '$ts'
}, {
    '$unwind': '$ts.cpu-load-all.cpu'
}, {
    '$group': {
        '_id': 0,
        'all-usr': {'$sum': '$ts.cpu-load-all.cpu.usr'}
    }
}])))
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda