• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

122
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?

almost 3 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'}
    }
}])))
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda