bc110402922
bc110402922

Reputation: 457

How I can replace the object key with root, in MongoDB

 "data": {
    "abc": {
      "Id": "100",
      "print": "number",
      "uploadAt" : "2021-22-01",
      "servicesAt" : "2020-01-12"



    },
    "xyz": {
      "Id": "123",
      "print": "number",
      "uploadAt" : "2021-22-01",
      "servicesAt" : "2020-01-12"
    }
}

Explanation I want to remove the root data, rest of the in side the data objects abc, xyz will remain as it is, in the 2nd step I want to replace the object value number with id key , I want to to do this dynamically,

Expected Output

{
    "abc": {
      "number": "100",
      "print": "number",
      "uploadAt" : "2021-22-01",
      "servicesAt" : "2020-01-12"
    },
    "xyz": {
      "number": "123",
      "print": "number",
      "uploadAt" : "2021-22-01",
      "servicesAt" : "2020-01-12"

    }
 }

Upvotes: 1

Views: 600

Answers (1)

Dheemanth Bhat
Dheemanth Bhat

Reputation: 4452

UPDATE based on comments

Solution #3:

db.testCollection.aggregate([
    {
        $addFields: {
            "array": { $objectToArray: "$data" }
        }
    },
    { $unwind: "$array" },
    {
        $addFields: { "array.v.number": "$array.v.Id" }
    },
    {
        $project: { "array.v.Id": 0 }                      // Optional 
    },
    {
        $group: {
            _id: "$_id",
            array: { $push: "$array" }
        }
    },
    {
        $replaceRoot: {
            newRoot: { $arrayToObject: "$array" }
        }
    }
]);

Solution #2: If you do not wat to rename filed Id to number then the solution is simple:

db.testCollection.aggregate([
    {
        $replaceRoot: {
            newRoot: {
                $arrayToObject: { $objectToArray: "$data" }
            }
        }
    }
]);

Output:

{
    "abc" : {
        "Id" : "100",
        "print" : "number",
        "uploadAt" : "2021-22-01",
        "servicesAt" : "2020-01-12"
    },
    "xyz" : {
        "Id" : "123",
        "print" : "number",
        "uploadAt" : "2021-22-01",
        "servicesAt" : "2020-01-12"
    }
}

OLD SOLUTION

Try this

db.testCollection.aggregate([
    {
        $replaceRoot: {
            newRoot: {
                $arrayToObject: {
                    $map: {
                        input: { $objectToArray: "$data" },
                        as: "item",
                        in: {
                            k: "$$item.k",
                            v: {
                                number: "$$item.v.Id",
                                print: "$$item.v.print",
                                uploadAt: "$$item.v.uploadAt",
                                servicesAt: "$$item.v.servicesAt"
                            }
                        }
                    }
                }
            }
        }
    }
]);

Output

{
    "abc" : {
        "number" : "100",
        "print" : "number",
        "uploadAt" : "2021-22-01",
        "servicesAt" : "2020-01-12"
    },
    "xyz" : {
        "number" : "123",
        "print" : "number",
        "uploadAt" : "2021-22-01",
        "servicesAt" : "2020-01-12"
    }
}

Upvotes: 2

Related Questions