Here's my data structure:
db.getCollection('competitionmatches').find()
{
"_id" : ObjectId("5d2d9eed5972a9367cd5010a"),
"matches" : [
{
"id" : 200000,
"utcDate" : "",
"minute" : null,
"homeTeam" : {
"id" : 808,
"coach" : {},
},
"goals" : [{},{}}],
},
{...},
{...},
],
"otherField": []
},
{...},
{...},
What i'm trying to remove are those fields: minute
, coach
and goals
in all matches
of all entries in competitionmatches
.
I've tried the following in Robo 3T with success:
db.getCollection('competitionmatches').update({}, {$unset: {"otherField":""}})
This removes well the whole array otherField
in each element of matches
if i pass multi:true
(removed here for the sake of readability).
But when i try:
db.getCollection('competitionmatches').update({}, {$unset: {"matches.$[].goals":""}})
or
db.getCollection('competitionmatches').update({}, {$unset: {"matches.$[].minute":""}})
i get a success message Updated 1 existing record(s) in 3ms
but the record stay the same, minute
or goals
do not get removed.
I got this answer from Remove a field from all elements in array in mongodb and updated my mongo version from 3.4 to 3.6 because the $[]
was introduced in 3.6 as said by svr and shown in the docs.
Unset works but the path seems to be the wrong part of my update.
"matches.$.goals"
returns an error and matches.[].goals
does not have any impact as well.
I was even wondering if there could be some cache but that does not make much sense since the unset on otherField
gets executed well.
I was also in doubt if the update went well but the app using the database works fine as far as i can see.