Changes in documents are auto reverted

Hi everyone,

I’m new to this community and MongoDB in general. I apologize if I’ve used incorrect tags. I’m facing a peculiar issue.

I have a script that updates the deviceName property of a Device object when a method is called. Here’s the relevant code:

`

@Override
public void handle(DeviceDTO deviceDTO) {
  var device = deviceService.getByDeviceId(deviceDTO.getDeviceId());
  var deviceData = deviceDTO.bindDataToClass(DeviceRenameData.class);
  var newName = deviceData.getName();
  var deviceInfo = device.getDeviceInfo();

  deviceInfo.setDeviceName(newName);
  device.setDeviceInfo(deviceInfo);

  deviceRepository.save(device);
}

`
This code works perfectly when connected to my local database. However, when I connect to a remote MongoDB M10 cluster, the changes to deviceName are reverted back within about 2 seconds.

I’ve reviewed the MongoDB logs, and here’s what I found:
`

Jul 26, 2024 8:38:21 AM org.example.Main main
INFO: Full Document: "Document{{_id=669f67863c6a102d7f2bad9c, deviceId=22e01a2c-b416-4988-a9ff-f67f4d31a546, tenantId=rahul, deviceStatus=Online, deviceInfo=Document{{ deviceName=TAKSHAK-PL, deviceModel=INTEL H110 Default string }} }}

Jul 26, 2024 8:38:30 AM org.example.Main main
INFO: Full Document: "Document{{_id=669f67863c6a102d7f2bad9c, deviceId=22e01a2c-b416-4988-a9ff-f66f4d31a546, tenantId=rahul, deviceStatus=Online, deviceInfo=Document{{deviceName=BREAKER, deviceModel=INTEL H110 Default string}} }}

Jul 26, 2024 8:38:32 AM org.example.Main main
INFO: Full Document: "Document{{_id=669f67863c6a102d7f2bad9c, deviceId=20e01a2c-b416-4988-a9ff-f67f4d31a546, tenantId=rahul, deviceStatus=Offline, deviceInfo=Document{{deviceName=TAKSHAK-PL, deviceModel=INTEL H110 Default string}} }}

`
As you can see, the deviceName is reverted back to “TAKSHAK-PL” after a short period. The interesting part is that this issue isn’t always reproducible; sometimes, the update persists.

I’d appreciate any insights you can offer on what might be causing this problem.

Hi @Takshak_PL and welcome to the community forum!!

It seems a bit unusual that the changes have been reverted.
Can you confirm if there has been no triggers or change streams enabled which reverts the changes when an update is made to the field value.

Could you also try and add more logger statement in the code to identify the changes?

Regards
Aasawari

1 Like

I verified that there are no triggers or change streams enabled on the database. Additionally, I’ve added more logging statements to the code for better tracking.

Here’s the updated code snippet:

`
@Override
public void handle(DeviceDTO deviceDTO) {
var device = deviceService.getByDeviceId(deviceDTO.getDeviceId());
var deviceData = deviceDTO.bindDataToClass(DeviceRenameData.class);
var newName = deviceData.getName();
var deviceInfo = device.getDeviceInfo();

deviceInfo.setDeviceName(newName);
device.setDeviceInfo(deviceInfo);

log.info(“Setting the name of device to {}”, newName);
deviceRepository.save(device);

var savedDevice = deviceRepository.findByDeviceId(device.getDeviceId())
.orElseThrow(() → new RuntimeException(“Device not found”));
log.info(“Logging the name after changing: {}”, savedDevice.getDeviceInfo().getDeviceName());
}
`

Server Log:

2024-08-02T13:48:30.263+05:45 INFO 3000 --- [boundChannel-10] c.s.m.d.user.DeviceRenameACKHandler : Setting the name of device to BEATRIX 2024-08-02T13:48:30.278+05:45 INFO 3000 --- [boundChannel-10] c.s.m.d.user.DeviceRenameACKHandler : Logging the name after changing: BEATRIX

The update persists in the database inconsistently. It works on my local machine with MongoDB, but not consistently on the production environment.