2 / 3
Jun 2024

My JWT payload, and therefore user.data, contains an array with some objectId string representations.

In a rule to validate write permissions, I want to compare the document _id with this array via the $in operator.

{ "_id": { "$in" { "%stringToOid": "%%user.data.myKey" } } }

Problem %stringToOid only seems to allow string and not an array of strings.

How to work around this issue ?

Thanks

Hi :wave: @Qt1,

Welcome to the MongoDB Community forums :sparkles:

I haven’t tested but could you try with $uwnind operator, it will deconstructs an array field from the input documents to output a document with the value of the array field replaced by the element.

Please keep the community posted, we’ll be happy to help.

Note: In case of further help please share the sample document, the aggregation query you tried, and the resultant output you expect.

Best regards,
Kushagra

Hi and sorry for my late response.

I’m not allowed to add attachement as I’m a new user. You’ll find a small sample of user.data and project collection add the end of this post.

I think my previous post may be a bit misleading. I’m using Atlas App services (mongo realm) and realm SDK on react-native side.
The App use CustomJWTProvider (https://www.mongodb.com/docs/atlas/app-services/authentication/custom-jwt/) to log into my App Services application and the JWT payload contains some information on projects (small array of ObjectId) that should be provided to the application.

I’m not sure I understand how to apply a $unwind in the “read” section a RuleExpression (https://www.mongodb.com/docs/atlas/app-services/rules/expressions/).
Can these behave like an AggregateQuery?

I still tried the following two possibilities, that are probably wrong, without success (no results are returned, no errors are returned in the app and in the AppService logs)

{ "_id": { "$in": { "%stringToOid": { "$unwind": "%%user.data.allowedProjects" } } } }
{ "_id": { "%stringToOid": { "$unwind": "%%user.data.allowedProjects" } } }

The following rules works as expected:

{ "idStr": { "$in": "%%user.data.allowedProjects" } }
{ "_id": { "%stringToOid": "%%user.data.test" } }

But the one I’d like to use doesn’t:

{ "_id": { "$in": { "%stringToOid": "%%user.data.allowedProjects" } } }

Here is a test JWT payload:

{ "test": "6622acd1cda914c2c9b307c1", "allowedProjects": ["6622acd1cda914c2c9b307c1", "665dcf4df09775c14a40a584"] }

Here is a test Projects collection

[{ "_id": { "$oid": "6622acd1cda914c2c9b307c1" }, "idStr": "6622acd1cda914c2c9b307c1", "name": "Test project 1" }, { "_id": { "$oid": "665dcf4df09775c14a40a584" }, "idStr": "665dcf4df09775c14a40a584", "name": "Test project 2" }]

Currently, as a workaround, I’m keeping a stringified version of my _id aside other properties to match it against my JWT.

Thanks again,
Quentin