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

0

273
Vistas
Embed root field in a subdocument within an aggregation pipeline

Maybe someone can help me with Mongo's Aggregation Pipeline. I am trying to put an object in another object but I'm new to Mongo and ist very difficult:

{
    "_id" : ObjectId("5888a74f137ed66828367585"),
    "name" : "Unis",
    "tags" : [...],
    "editable" : true,
    "token" : "YfFzaoNvWPbvyUmSulXfMPq4a9QgGxN1ElIzAUmSJRX4cN7zCl",
    "columns" : [...],
    "description" : "...",
    "sites" : {
            "_id" : ObjectId("5888ae2f137ed668fb95a03d"),
            "url" : "www.....de",
            "column_values" : [
                    "University XXX",
                    "XXX",
                    "false"
            ],
            "list_id" : ObjectId("5888a74f137ed66828367585")
    },
    "scan" : [
            {
                    "_id" : ObjectId("5888b1074e2123c22ae7f4d3"),
                    "site_id" : ObjectId("5888ae2f137ed668fb95a03d"),
                    "scan_group_id" : ObjectId("5888a970a7f75fbd49052ed6"),
                    "date" : ISODate("2017-01-18T16:00:00Z"),
                    "score" : "B",
                    "https" : false,
                    "cookies" : 12
            }
    ]
}

I want to put every object in the "scan"-array into "sites". So that it looks like this:

{
    "_id" : ObjectId("5888a74f137ed66828367585"),
    "name" : "Unis",
    "tags" : [...],
    "editable" : true,
    "token" : "YfFzaoNvWPbvyUmSulXfMPq4a9QgGxN1ElIzAUmSJRX4cN7zCl",
    "columns" : [...],
    "description" : "...",
    "sites" : {
            "_id" : ObjectId("5888ae2f137ed668fb95a03d"),
            "url" : "www.....de",
            "column_values" : [
                    "University XXX",
                    "XXX",
                    "false"
            ],
            "list_id" : ObjectId("5888a74f137ed66828367585"),
            "scan" : [
            {
                    "_id" : ObjectId("5888b1074e2123c22ae7f4d3"),
                    "site_id" : ObjectId("5888ae2f137ed668fb95a03d"),
                    "scan_group_id" : ObjectId("5888a970a7f75fbd49052ed6"),
                    "date" : ISODate("2017-01-18T16:00:00Z"),
                    "score" : "B",
                    "https" : false,
                    "cookies" : 12
            }
        ]
    }
}

Is there a step in the aggregation pipeline to perform this task?

over 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

With a single pipeline I don't see any other way but specifying each field individually as:

db.collection.aggregate([
    {
        "$project": {
            "name": 1, "tags": 1,
            "editable": 1,
            "token": 1, "columns": 1,
            "description": 1,
            "sites._id": "$sites._id",
            "sites.url": "$sites.url" ,
            "sites.column_values": "$sites.column_values" ,
            "sites.list_id": "$sites.list_id",
            "sites.scan": "$scan"
        }
    }
])

With MongoDB 3.4 and newer, you can use the $addFields pipeline step instead of specifying all fields using $project. The advantage is that it adds new fields to documents and outputs documents that contain all existing fields from the input documents and the newly added fields:

db.collection.aggregate([
    {
        "$addFields": {
            "sites._id": "$sites._id",
            "sites.url": "$sites.url" ,
            "sites.column_values": "$sites.column_values" ,
            "sites.list_id": "$sites.list_id",
            "sites.scan": "$scan"
        }
    }, { "$project": { "scan": 0 } }
])
over 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