Home > Software engineering >  mongo aggregation bring field front
mongo aggregation bring field front

Time:09-16

i have complex code using mongo aggregation to achive certain task now i get like

[
    {
        "data": [
            {
                "_id": "630c953a90a5ad89e4296f18",
                "educations": [],
                "benefits": [],
                "is_deleted": false,
                "meta_skills": [
                    "62319b5a751781cbc7c921ab",
                   ],
                "click_count": 0,
                "view_count": 0,
                "createdAt": "2022-08-29T18:30:00.000Z",
                "updatedAt": "2022-08-29T18:30:00.000Z",
                
            },

i need a pipeline after that it should return array inside data as final result basically wanna get ride of outer array and curly braces

CodePudding user response:

You can use $unwind and $replaceRoot, like this:

db.collection.aggregate([
  {
    "$unwind": "$data"
  },
  {
    "$replaceRoot": {
      "newRoot": "$data"
    }
  }
])

Here's the playground link.

  • Related