Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda