Unable to set up Sync Permissions properly

Hi, I am unable to set up Rules for Atlas app services properly.

Now, I have 2 collections: Users and Groups

  • A User contains _id and an array of ObjectId referencing groups they are part of
  • A Group contains _id, creator_id and an array of ObjectId, trip_members referencing members in the group

When it is readAllWriteAll on Groups, I can run functions which run as user which created the group without a problem.

Now, when I change to

{
  "creator_id": "%%user.custom_data._id”
},
{
  “trip_members”: { “$all" : [" %%user.custom_data.id" ] }
}

or

{
  "creator_id": "%%user.id"
},
{
  “trip_members”: { “$all" : [ "%%user.id" ] }
}

for both read and write, the query which previously works now returns null.

Unsure where I am missing since this is also an example in the guides but does not seem to work

Hi, can you try updatijng it to {“trip_members”: "%%user.custom_data.id"}. I believe you have two issues.

  1. You have a space in this string " %%user.custom_data.id"
  2. There is no need to use $all for an array with a single element, just use the basic ANY approach which is much much easier to express as {“trip_members”: "%%user.custom_data.id"}

Best,
Tyler

Hi, thanks for the quick reply.

About the syntax issue, I did fix it but the issue actually came from the following:

  • creator_id was stored as ObjectId in my schema.

  • Looking at add-collaborators sample application in your docs, I realised that examples of owner_id actually uses string. So I am assuming that %%user.id is expanding into a string? When I switched to checking with string fields, it works.

“email” : “%%user.custom_data.email"

Then, what would be the best practice?

  1. Introducing redundant field: creator_id is user._id.toString()
  2. Using another queryable field, such as email, and using Array<String> instead of Array<ObjectId>

For 2. Thanks for the tip! Will do.

Cheers

Oh, glad to know you figured it out. For that, we have these expressions you can use to translate the value in the expression: https://www.mongodb.com/docs/atlas/app-services/rules/expressions/#convert-ejson-values

So you could do something like {“trip_members”: {"%stringToOid": "%%user.custom_data.id"}}

Got it! Thank you so much!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.