【问题标题】:Mongo Java Spring compound indexes will not set, and it clears previously setting indexes [duplicate]Mongo Java Spring复合索引不会设置,它会清除以前设置的索引[重复]
【发布时间】:2015-11-27 10:48:14
【问题描述】:

我搜索了如何在 Java Spring Data MongoDB 中设置复合索引。 SO和其他网站说添加:

@CompoundIndex(name = "test_name", def = "{'subDeviceIndex : 1, 'sensorIndex : 1'}")

或者

@CompoundIndexes({
    @CompoundIndex(name = "test_name", def = "{'subDeviceIndex : 1, 'sensorIndex : 1'}")
})

@Document 注释上方(我在上面和下面都尝试过)应该保存复合索引,但是它不会保存复合索引并清除我现有的索引。在下面找到该类的 sn-p。集合正确保存,我可以提取数据 sn-p 只显示变量声明和注释。类的其余部分是 getter/setter 和 @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;

包含以下问题以供参考,因为它们没有解决问题:

How to use spring data mongo @CompoundIndex with sub collections?

@CompoundIndex not working in Spring Data MongoDB

谢谢!

【问题讨论】:

    标签: java spring mongodb


    【解决方案1】:

    我在 spring-data-mongodb 中使用了复合索引注释,并且该注释确实对我有用。共享功能代码段。

    @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
    

    即使复合索引中涉及的字段之一被注释为索引,两个索引共存,如下所示:

    @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
        }
    ]
    

    【讨论】:

    • 如果您使用@Indexed,这对您仍然有效吗?
    • 嗨 Hughzi,测试了在复合索引所涉及的字段上添加 @Indexed 的索引,它仍然有效。正如您所看到的,这两个索引是共存的,如我的编辑中所述。
    • 我已标记为正确并已投票,感谢您的回复!我的问题是放错了单引号。您的答案确实在没有意识到的情况下解决了问题。我已将问题标记为重复。我觉得这个问题已经得到了回答,希望你的代表能最后一次通过删除。
    • 还有一点要记住的是,只有在插入至少一个介绍索引的文档后才会创建索引。
    猜你喜欢
    • 2022-01-22
    • 2019-09-16
    • 2013-05-09
    • 1970-01-01
    • 2021-10-30
    • 2015-04-17
    • 2014-09-09
    • 2017-07-04
    相关资源
    最近更新 更多