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

310
Visualizações
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 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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