I am trying to convert a simple array into an array of arrays while retaining the existing values:
roles: Array
["mentor",
"engineer"]
into this:
roles: Array
[name: "mentor",
startDate: "01-01-1900",
endDate: "12-31-2099"],
[name: "engineer",
startDate: "01-01-1900",
endDate: "12-31-2099"]
Everything I have tried wipes out my existing values.
Thanks
Hello @Austin_Summers ,
Your question is not clear to me, what are you trying to do:
Do you want to update the existing document’s field?
Do you want to format the result by a find/aggregation query?
Where are these startDate and endDate coming from? Is it external input or internal fields?
FYI: Your expected result array is not a valid array/object.
It will help us if you post an example document, and whatever query that you tried.
Hello @turivishal ,
I am looking to reformat the existing data structure without losing the existing data.
This will be part of a larger set of updates to change and add other fields.
The fields will eventually be updated by a tool in current development.
I tried a $map that added the date fields, but wiped out the existing data.
db.resources.updateOne(
{ name: "Jon Doe" },
[{
$set: {
roles: {
$map: {
input: "$roles",
in: {
name: "$$this.name",
startDate: null,
endDate: null
}
}
}
}
}]
)
Austin_Summers:
name: “$$this.name”,
here you need to just use "$$this" instead of "$$this.name" because roles existing value has an array of strings.
1 Like
Thank you!
I just tried that myself. I am still very fuzzy on the use of $$this within aggregations.
Thanks again.
Austin
1 Like
Look at the documentation of $map aggregation array operator,
It is explained everything perfectly with the example.
1 Like
system
(system)
Closed
July 4, 2022, 4:24pm
8
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.