Hi
I am fairly new to MongoDB and I have a strange issue when trying to update an account
object nested under user
object, using a custom resolver. Executing query remotely (e.g. from Postman) fails 1 in 3 times, while executing it through Realm UI is always successful and is 10 times faster.
Setup:
The ‘Users’ collection holds user
objects that look like this:
{
_id:...
externalId:...
account: {
...
...
}
}
I wanted to be able to update one or more fields in the account
nested object, without the need to fetch the object first, building a new account
object and then using a standard UpdateOneUser
resolver to finally perform the operation.
I build a custom resolver:
exports = async function ({ externalId, accountPartial }) {
const cluster = context.services.get("mongodb-atlas");
const users = cluster.db("main-db").collection("Users");
const keys = Object.keys(accountPartial)
const user = await Promise.all(keys.map(key => {
const obj = {}
obj[`account.${key}`] = accountPartial[key]
return users.findOneAndUpdate(
{ externalId: externalId},
{ $set: obj },
{ returnNewDocument: true }
)
}));
return user[0];
}
and I am calling it via the following mutation:
mutation updateUserAccountPartial($externalId: String!, $accountPartial: InputAccountPartial!) {
user: custom__userAccountPartialUpdate(
input: {externalId: $externalId, accountPartial: $accountPartial}
) {
_id
}
}
Results:
Failed request
There is no problem whatsoever when executing the resolver directly or via the GraphQL UI in the dashboard. When I try to call it via Postman however, on average 1 out of 3 times I get an error:
{
"message": "failed to update documents: retry count exhausted",
"locations": [
{
"line": 1,
"column": 116
}
],
"path": [
"user"
]
}
and in the GraphQL log I have:
Status: OK
Runtime: ~4000ms
…
Rule Performance Metrics:
{
"main-db.Users": {
"roles": {
"server": {
"matching_documents": 202,
"evaluated_fields": 0,
"discarded_fields": 0
}
},
"no_matching_role": 0
}
}
I have only 2 documents in the Users
collection, so the matching_documents
at 202 is a strange number.
Successful request
For a successful operation, the GraphQL logs are:
Status: OK
Runtime: ~2000ms
…
Rule Performance Metrics:
{
"main-db.Users": {
"roles": {
"server": {
"matching_documents": 123,
"evaluated_fields": 0,
"discarded_fields": 0
}
},
"no_matching_role": 0
}
}
Problem summary
When I run the mutation from GraphQL UI or I run the custom resolver function directly, the runtime is approx: 300ms, and it always succeeds.
Could anyone help me figure out:
- Why are the execution times so long?
- Why are they failing at random.
- Why is the
matching_documents
field so big - How to fix it
I will be very grateful for all help.