support try..catch
It does not appear that mongosh supports try..catch. I put catch block around "insertOne"
try {
db.inboxItemCounts.insertOne({
"_id": {
"userId": userDoc.userId
},
"fromUserSummary": [{
"userName": fromUserDoc.userName,
"count": 1
}],
"statusSummary": [{
"status": fromUserDoc.status,
"count": 1
}]
});
}
catch(err) {
// print(err);
if(err.includes("E11000 duplicate key error collection")) {
print("got duplicate key exception");
}
}
and the "E11000 duplicate key error collection" exception was not caught at all.
The script stopped and printed the whole error message.
-
AdminMassimiliano (Admin, MongoDB) commented
That does indeed work, with a little adjustment.
try { db.test.insertOne({_id: -1}) } catch(e) {
if(e.message.includes("E11000 duplicate key error collection")) {
print("got duplicate key exception");
}
} -
Steve commented
Nevermind