Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!