I’m learning mongo shell, using the compass UI. I am trying to return the document _id so I can then rename a field within an array of that document.
Firstly, I was trying to get the doc _id but I can’t get it to print in the shell.
I tried targeting it as so
doc._id
all i see in the console is {}.
My other question, the real question is I’m trying to rename a field in an object, that is embedded in an array. my thought was I could get the specific doc ids, then update those docs using a foreach or something.
I’ve spent many hours trying many different tactics but I end up with one of two things happening:
- all documents are updated
- Only the correct documents are updated but not correctly, for example the ‘new service’ field is added to all service categories not just service_category_1.service_subcategory_2
here is my basic command from the shell:
db.collection.find({"services.tags.region1.service_category_1.service_subcategory_2": {$exists: 1}})
.forEach( function( doc ) {
if(doc.services){
doc.services.forEach(function (service){
//here, for each instance of
//services.tags.region1.service_category_1.service_subcategory_2
//I want to rename all instances of 'service_subcategory_2' to 'new_service'
})
}
}
)
I also tried aggregation but I’m not wrapping my head around how it’s supposed to work. I looked here, How to rename a field name inside nested array?
Here is some sample data for one doc:
[{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b01"
},
"services": [
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b1b"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_2": "true"
},
"service_category_2": {
"service_subcategory_3": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b1d"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_2": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b1f"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_4": "true",
"service_subcategory_2": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b22"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_4": "true"
},
"service_category_2": {
"service_subcategory_3": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b27"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_5": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b2c"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_5": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b2e"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_5": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b30"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_2": "true"
}
}
},
},
{
"_id": {
"$oid": "5e7e4bd9d54f1760921a3b32"
},
"tags": {
"region_1": {
"service_category_1": {
"service_subcategory_2": "true"
}
}
},
},
{
"_id": {
"$oid": "61ea350aad77ef002ef20fd8"
},
"tags": {
"region_1": {
"service_category_3": "true"
}
},
}
]
}]