sql >> Base de Datos >  >> NoSQL >> MongoDB

Proyecte diferentes estructuras incrustadas con el mismo nombre

Puede usar el $ifNull operador a $project el campo "nombre de host".

cmp = db['computers'].aggregate([
    {"$project": { 
        "u_hostname": {
            "$ifNull": [
                "$hostnames.name", 
                { "$map": { 
                    "input": {"$literal": ["A"]}, 
                    "as": "el", 
                    "in": "$hostname"
                }}
            ]
        }, 
        "_id": 0, 
        "u_ipv4": "$addresses.ipv4"
    }},
    {"$unwind": "$u_hostname"}
])