insertOne
should return an Object
like this if successful
{
acknowledged: true,
insertId: <object OjectId> // or the _id you specified
}
updateOne
should return an Object
like this if successful
{
acknowledged: true,
modifiedCount: 1, // or 0 if upserted
upsertedId: null, // or an <object OjectId>
upsertedCount: 0, // or 1 if upserted
matchedCount: 1 // or 0 if upserted
}
Now, updateOne
only updates the first document that matched the query. If no document matched the query, it would simply update no document and throw no errors. You would get an object like this if that happens,
{
acknowledged: true,
modifiedCount: 0,
upsertedId: null,
upsertedCount: 0,
matchedCount: 0
}
So, checking data.result.ok
would tell you nothing in that scenario. You have to manually check each method call if you want to something like that. In your case, maybe do something like this?
execTransaction(session => {
return async () => {
await Promise.all([
db.collection('table1').updateOne({ username: username }, { $push: { apps: client_id } }, { session }),
db.collection('table2').insertOne({ data: data }, { session })
])
};
}, data => {
if (data[0].matchedCount === 0) {
// do error handling ?
}
}
)
Or, if you want to know if all the CRUD operations succeeded as the title suggests, maybe this?
(await Promise.all([a list of all insertOne and updateOne operations... ])
.every(obj => obj && obj.acknowledged && (obj.insertId || obj.modifiedCount || obj.upsertedCount))
// this checks if all the properties are non zero
// thus if It's true, everything succeed
But again I would suggest you to do error handling separately for each operation when needed.