Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Will Not Implement
Categories MongoDB Shell
Created by Guest
Created on Sep 22, 2022

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.
  • Guest
    Oct 10, 2022
    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"); } }
  • Guest
    Sep 22, 2022
    Nevermind