Spring boot/mongo 不会使用索引注释创建索引 [英] Spring boot / mongo wont create index with the index annotation

查看:33
本文介绍了Spring boot/mongo 不会使用索引注释创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

@Document(collection = "linkmetadata")
public class LinkMetaData {
@Indexed(unique = true)
private String url;
...
}

但是每当它创建集合时,它都不会为 url 字段创建任何索引,就像它只是忽略了注释.知道这是为什么吗?

But whenever it creates the collection it doesn't create any index for the url field, it's like it just ignores the annotation. Any idea why this is?

插入数据时也不会创建索引.当我尝试获取特定 url 的数据时,如果我两次输入相同的 url,它会抛出一个错误,即 url 键不是唯一的,但它并不关心插入唯一键,因为没有索引..

no index is created upon insertion of data either. And when I try to fetch data for a specific url it throws an error that the url key is not unique if I entered the same url twice, but it doesn't care about inserting the unique key since there is no index..

推荐答案

我发现了问题.我还有另一个集合,其中 url 字段标记为 unqiue.我必须在其中之一上指定索引的名称,否则它似乎认为该索引已经存在,即使它位于两个不同的集合中

I found the problem. I had another collection also with a url field marked as unqiue. I had to specify the name of the index on one of them otherwise it seems that it considered that the index already exists even though it was on two different collections

@Indexed(name = "meta_url_index_unique", unique = true)
private String url;

这篇关于Spring boot/mongo 不会使用索引注释创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆