I have the following employee data structure :
{
"_id":{
"$oid":"62836b0b3c1e2c19eaaac07e"
},
"firstname":"jean",
"lastname":"paul",
"email":"[email protected]",
"password":"d94307e0eabb9bba1ea1291e4b043175e11dcc43875a4858690e5c236b989f3e",
"salt":"2CzpCiIbiyYcSxQ",
"organizations":[
{
"organizationid":{
"$oid":"62836aa93c1e2c19eaaac07d"
},
"organizationname":"test",
"ismanager":false,
"groups":[
{
"name":"mongroupe",
"permissions":[
0,
2
]
}
],
"permissions":[
1
]
}
]
}
I'm trying to build a function in go that adds a set of permissions to the organizations.[x].groups.[x].permissions array. Each permission is a simple integer.
I've come up with the following code :
func AddPermissionsToGroup(groupName string, organizationId primitive.ObjectID, permissions []global.Permission) error {
if res, err := employeesCollection.UpdateMany(
context.TODO(),
bson.M{},
bson.M{
"$addToSet": bson.M{
"organizations.$[org].groups.$[grp].permissions": bson.M{
"$each": permissions,
},
},
},
options.Update().SetArrayFilters(
options.ArrayFilters{
Filters: []interface{}{
bson.M{
"org.organizationid": organizationId,
},
bson.M{
"grp.name": groupName,
},
},
},
),
); res.MatchedCount == 0 {
return global.ErrEntityNotFound
} else if res.ModifiedCount == 0 {
return global.ErrNoUpdateNeeded
} else if err != nil {
return fmt.Errorf("failed updating employees | %s", err.Error())
}
return nil
}
The code returns the following error : write exception: write errors: [Cannot apply array updates to non-array element groups: null].
I have no idea what it means.
Maybe the engine doesn't like the fact that I use two ArrayFilters ?