Home > Software design >  Get the nested value of aggregation result at top level
Get the nested value of aggregation result at top level

Time:10-10

I made an aggregation query in MongoDB.

{
    "productData" : {
        "_id" : ObjectId("634020b72f772f0f828eadaa"),
        "name": "shoe",
        "price": 450
    }
}

but how can I get just the nested part in my query result like

{
    "_id" : ObjectId("634020b72f772f0f828eadaa"),
    "name": "shoe",
    "price": 450
}

Which aggregation stage should I use?

CodePudding user response:

Use $replaceWith stage to replace the input document with productData document.

db.collection.aggregate([
  {
    $replaceWith: "$productData"
  }
])

Demo @ Mongo Playground

  • Related