11

I have a document similar to the following, from which I want to return the sub-fields of the current top level field as the top level fields in every document of the results array:

{ 
  field1: {
    subfield1: {},
    subfield2: [],
    subfield3: 44,
    subfield5: xyz
  },
  field2: {
    othercontent: {}
  }
}

I want the results of my aggregation query to return the following (the contents of field1 as the top level document):

{
  subfield1: {},
  subfield2: [],
  subfield3: 44,
  subfield5: xyz
}

Can this be done with $project and the aggregation framework without defining every sub fields to return as a top level field?

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Gary Sharpe
  • 2,369
  • 8
  • 30
  • 51
  • 2
    I'm curious if that's possible; I tend to think it's not. This question seems related: http://stackoverflow.com/q/19431773/390819 – Cristian Lupascu Oct 17 '13 at 21:21

3 Answers3

3

You can use $replaceRoot aggregation operator since 3.4:

db.getCollection('sample').aggregate([
    {
        $replaceRoot: {newRoot: "$field1"}
    }
])

Provides output as expected:

{
    "subfield" : {},
    "subfield2" : [],
    "subfield3" : 44,
    "subfield5" : "xyz"
}
Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49
1

It's generally hard to make MongoDB deal with ambiguous or parameterized json keys. I ran into a similar issue and the best solution was to modify the schema so that the members of the subdocument became elements in an array.

However, I think this will get you close to what you want (all code should run directly in the Mongo shell). Assuming you have documents like:

db.collection.insert({
  "_id": "doc1",
  "field1": {
           "subfield1": {"key1": "value1"},
           "subfield2": ["a", "b", "c"],
           "subfield3": 1,
           "subfield4": "a"
         },
  "field2": "other content"
})

db.collection.insert({ 
  "_id": "doc2",
  "field1": {
           "subfield1": {"key2": "value2"},
           "subfield2": [1, 2, 3],
           "subfield3": 2,
           "subfield4": "b"

         },
  "field2": "yet more content"
})

Then you can run an aggregation command that promotes the content of field1 while ignoring the rest of the document:

db.collection.aggregate({
"$group":{
    "_id": "$_id",
    "value": {"$push": "$field1"}
}})

This makes all the subfield* keys into top-level fields of an object, and that object is the only element in an array. It's clumsy, but workable:

"result" : [
        {
                "_id" : "doc2",
                "value" : [
                        {
                                "subfield1" : {"key2" : "value2"},
                                "subfield2" : [1, 2, 3],
                                "subfield3" : 2,
                                "subfield4" : "b"
                        }
                ]
        },
        {
                "_id" : "doc1",
                "value" : [
                        {
                                "subfield1" : {"key1" : "value1"},
                                "subfield2" : ["a","b","c"],
                                "subfield3" : 1,
                                "subfield4" : "a"
                        }
                ]
        }
],
"ok" : 1
Community
  • 1
  • 1
SuperAce99
  • 712
  • 6
  • 13
0

Starting Mongo 4.2, the $replaceWith aggregation operator can be used to replace a document by another (in our case by a sub-document) as syntaxic sugar for $replaceRoot:

// { field1: { a: 1, b: 2, c: 3 }, field2: { d: 4, e: 5 } }
// { field1: { a: 6, b: 7 }, field2: { d: 8 } }
db.collection.aggregate({ $replaceWith: "$field1" })
// { a: 1, b: 2, c: 3 }
// { a: 6, b: 7 }
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190