Home > other >  Remove id form embeded documents
Remove id form embeded documents

Time:03-31

Hi i have embedded document like this: I want to remove "id_group" of worker because its unnecessary and change format date to correct. I need agregate function to to this? What is easy way to do this?

{
    "_id": {
        "$oid": "6242d36461e63768f0ac1529"
    },
    "id_z": 10,
    "name": "Journalists",
    "addres": "New York",
    "workers": {
        "worker": [{
            "id_worker": 15,
            "name": "example",
            "job": "example",
            "id_director": 25,
            "hired": "2005-02-20",
            "id_group": 10
        }, {
            "id_worker": 16,
            "name": "example",
            "job": "example",
            "id_director": 26,
            "hired": "2005-02-20",
            "id_group": 10
        }]
    }
}

CodePudding user response:

If you want to remove the field from the DB you can use $unset like this:

db.collection.update({},
{
  $unset: {
    "workers.worker.$[].id_group": ""
  }
},
{
  "multi": true
})

And this query will remove all id_group from all objects.

Example here

Also, what if you want is to not output the value simply use projection and the value will not be shown but will be still in DB.

db.collection.find({},
{
  "workers.worker.id_group": 0
})

Example here

And for aggregations queries you can use $project in the same way... example

  • Related