2

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 ?

1 Answer 1

1

Ok so i created this playground that works : https://mongoplayground.net/p/Tznz7btfLkH

Actually the issue was not because of the request itself that was valid syntaxically. It came from the fact i had another document in the employee collection that looked like this :

{
   "_id":{
      "$oid":"62836aa93c1e2c19eaaac07c"
   },
   "firstname":"jean",
   "lastname":"charles",
   "email":"[email protected]",
   "password":"6776c354fce809e7be234f84d0ea907cfd3aba350871a18858ceccc10eabc036",
   "salt":"IHaPJIKVVplC8ni",
   "organizations":[
      {
         "organizationid":{
            "$oid":"62836aa93c1e2c19eaaac07d"
         },
         "organizationname":"test",
         "ismanager":true,
         "groups":null,
         "permissions":null
      }
   ]
}

As you can see, the groups value is null here. And that is the problem.

Basically, when the arrayfilter named grp meets a null object, it raises an error because it can't apply on it.

So the solution to make the request work was to make sure every document in the collection had this group array initialized (even if it's empty, it doesn't matter). Once I did this there was no problem anymore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.