3

I have the async function below. DeviceStatus is a Mongo Collection of devices which has the property SerialNumber defined and NumberOfRetries defined. I want to take in as an input a serialNumber and retries. serialNumber is a string that is known to be in the collection. retries is a string representing an int less than 10. In the async function below I'd like to read the retries value, add 1 to it and then update the mongo document with the same serialNumber

static async incrementNumberOfRetries(serialNumber, retries){
  const retriesInt = parseInt(retries);
  const accumulatedRetriesInt = retriesInt + 1;
  const accumulatedRetriesString = accumulatedRetriesInt.toString();
  try {
    await DeviceStatus
      .updateOne({
        "SerialNumber": serialNumber
        },
        {
          $set: {
            "NumberOfRetries" : accumulatedRetriesString
        }
      })
  } catch(error) {
    console.log(error)
  }
}

I'm not entirely sure what I'm doing wrong here. I know the first 3 lines are correct and there's no error being caught. But the NumberOfRetries is not being set properly in the collection. Initially, it's been set to 1 so I've tested this function and the value is not changing. Is there something wrong with how I'm using updateOne? How can I pass in a variable to perform the update?

5
  • But the NumberOfRetries is not being set properly in the collection. Could you please provide more information on this ? The value isn't changed? Commented Apr 6, 2021 at 18:46
  • Correct, the value is not updating. Currently, it's initialized as 0 so it's defined. It's just not incrementing. Commented Apr 6, 2021 at 18:47
  • So, is the field SerialNumber unique in your dataset? I mean, is there only 1 document for a SerialNumber? If there are more than 1, your query will update only the first document if finds from the database Commented Apr 6, 2021 at 18:51
  • SerialNumber is unique Commented Apr 6, 2021 at 18:51
  • I fixed it... The query is correct and works fine. I have a very similar collection where DeviceId is used instead of SerialNubmer. I had to be using DeviceId instead of SerialNubmer. Thanks, @ĐăngKhoaĐinh, for your responses. Upon further investigation everything is working now Commented Apr 6, 2021 at 18:57

1 Answer 1

2

To increment the numberOfRetries, you can use MongoDB update operator $inc.

DeviceStatus.update({
    serialNumber,
  },
  {
    $inc: {
      numberOfRetries: 1
  }
})

See a working example here on MongoDb Playground

Your code should look like this

static async incrementNumberOfRetries(serialNumber, retries){
  try {
    await DeviceStatus
      .update({
        "SerialNumber": serialNumber
        },
        {
          $inc: {
            "NumberOfRetries" : 1
        }
      })
  } catch(error) {
    console.log(error)
  }
}

Check out the docs for the $inc operator here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.