I have searched for how to set a compound index in Java Spring Data MongoDB. SO and other sites says that adding:
@CompoundIndex(name = "test_name", def = "{'subDeviceIndex : 1, 'sensorIndex : 1'}")
Or
@CompoundIndexes({
@CompoundIndex(name = "test_name", def = "{'subDeviceIndex : 1, 'sensorIndex : 1'}")
})
Above the @Document
annotation (I have tried above and below) should save the compound index, however it does not save the compount index and clears my existing indexes. Find below a snippet of the class. The collection saves correctly and I can pull data the snippet just shows variable declarations and annotations. The rest of the class is getters/setters and an @PersistenceConstructor
.
@Document(collection=SensorValueDAOImpl.COLLECTION)
public class SensorValue {
@Id
private String id;
@DBRef
@Indexed
private RootDevice rootDevice;
//0 means root device
@Indexed
private int subDeviceIndex;
//sensor number, starting from 0
@Indexed
private int sensorIndex;
private int presentValue;
private int lowestDetectedValue;
private int highestDetectedValue;
private int recordedValue;
private Date dateTime;
The following questions are included for reference as they did not solve the issue:
How to use spring data mongo @CompoundIndex with sub collections?
@CompoundIndex not working in Spring Data MongoDB
Thanks!