Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1305

Database operations not always working in Classes

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • No version
    • Affects Version/s: 1.6.0
    • Component/s: AsyncWriter
    • None
    • Needed
    • Hide

      https://www.mongodb.com/docs/mongodb-shell/reference/compatibility/#limitations-on-database-calls should list JS setters as places in which database calls cannot be used, in addition to the already-mentioned cases of generator functions and constructors.

      Show
      https://www.mongodb.com/docs/mongodb-shell/reference/compatibility/#limitations-on-database-calls should list JS setters as places in which database calls cannot be used, in addition to the already-mentioned cases of generator functions and constructors.

      I try to migrate my scripts to new mongosh, but is seems to be still a nightmare...

       

      I have a logging framework like this:

      class Logger {
         constructor(logId) {
            Object.defineProperty(this, 'logId', { value: logId });
            Object.defineProperty(this, 'action', { value: -1, writable: true });
         }   
      
      set Command(cmd) {
            db.logging.updateOne({ _id: this.logId }, { $set: { [`action.${this.action}.command`]: cmd } });
         }   
      
      unsetCommand(i, duration) {
            db.logging.updateOne({ _id: this.logId }, { $set: { [`action.${i}.duration`]: duration }, $unset: { [`action.${i}.command`]: null } });
         }}
      
      var doc = db.logging.insertOne({
         script: "updateStats",
         timestamp: ISODate(),
         action: [],
         state: 'running'
      });
      var logger = new Logger(doc.insertedId);
      
      for (let i = 0; i < 10; i++) {
         db.logging.findOneAndUpdate({ _id: logger.logId }, { $push: { action: { ts: ISODate(), name: "connectReplicaSet" } } });
         logger.action = i;
         logger.Command = `command #${i}`;
         logger.unsetCommand(i, 11);
      }
      db.logging.updateOne({ _id: logger.logId }, { $set: { duration: 123, state: 'success' } });
      
      db.logging.findOne({ _id: logger.logId }).action.filter(x => x.command !== undefined)

      The last command {{db.logging.findOne(

      { _id: logger.logId }

      ).action.filter(x => x.command !== undefined)}} typically returns a few documents, i.e. {{$unset: { [`action.${i}.command`]: null } }} did not work.

      Typically on 20-50% of the commands the update is not written to database.

       

      I tried with different writeConcern and {{(async () =>

      { db.logging.updateOne(...) }

      )()}} but none of them is working reliable. In legacy mongo shell it is always working fine.

       

      Any suggestions?
      Wernfried Domscheit

            Assignee:
            Unassigned Unassigned
            Reporter:
            wernfried.domscheit@sunrise.net Wernfried Domscheit
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: