2

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!

Community
  • 1
  • 1
Hughzi
  • 2,470
  • 1
  • 20
  • 30

1 Answers1

3

I have used compound index annotation with spring-data-mongodb and the annotation does work for me. Sharing the functional code segment.

@Document
@CompoundIndexes(value = { @CompoundIndex(name = "post_vote_user_idx", def = "{'postId':1, 'user':1}", unique = true) })
public class PostVote implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 6690216554403820228L;
    private String id;
    @NotEmpty
    private String postId;
    @NotEmpty
    private String ownerEmail;
    @NotEmpty
    private String postTypeCode;
    private String parentId;
    @NotEmpty
    private String voteTypeId;
    @NotNull
    @DBRef
    private User user;
    // Rest of the class follows

Even when one of the field involved in compund index is annotated as Indexed, both indices co-exist as shown:

@Document
@CompoundIndexes(value = { @CompoundIndex(name = "post_vote_user_idx", def = "{'postId':1, 'user':1}", unique = true) })
public class PostVote implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 6690216554403820228L;
    private String            id;
    @Indexed
    private String            postId;
    @DBRef
    private User              user;

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "mongotest.postVote"
    },
    {
        "v" : 1,
        "unique" : true,
        "key" : {
            "postId" : 1,
            "user" : 1
        },
        "name" : "post_vote_user_idx",
        "ns" : "mongotest.postVote",
        "sparse" : false
    },
    {
        "v" : 1,
        "key" : {
            "postId" : 1
        },
        "name" : "postId",
        "ns" : "mongotest.postVote",
        "sparse" : false
    }
]
Abhishek Gupta
  • 320
  • 2
  • 12
  • does this still work for you if you use @Indexed? – Hughzi Nov 27 '15 at 11:23
  • Hi Hughzi, Tested the index adding @Indexed on the field involved in compound index as suggested and it still works. As you can see both the indexes are co-existing as described in my edit. – Abhishek Gupta Nov 27 '15 at 12:02
  • I have marked as correct and up-voted thank you for replying! my problem was misplaced single quotes. your answer does solve the problem without realizing. I have flagged the question as duplicated. i feel this has been answered already hopefully your rep will last passed the deletion. – Hughzi Nov 27 '15 at 12:10
  • One more thing to keep in mind is the indexes will be created only after inserting atleast a single document post introducing them. – Biscuit Coder Mar 31 '17 at 07:47