lap*_*ots 6 mongodb spring-data-mongodb spring-boot
我有一个document看起来像这样
@Document
public @Data class Note {
@Id
private String noteId;
private String owner;
@TextIndexed
private String name;
@TextIndexed
private String text;
private List<String> tags;
private LocalDate date;
}
Run Code Online (Sandbox Code Playgroud)
我也spring data mongodb用来操作mongodb数据存储。我已经创建了界面
public interface NoteRepository extends MongoRepository<Note, String> {
List<Note> findByTags(List<String> tags);
}
Run Code Online (Sandbox Code Playgroud)
我存储的对象看起来像这样
[
{
"noteId": "594e4adc3bc5152218f933b4",
"owner": "system",
"name": "simple note",
"text": "My text",
"tags": [
"tag1",
"tag2",
"tag3"
],
"date": [
1992,
12,
15
]
}
]
Run Code Online (Sandbox Code Playgroud)
但是,除非我不提供标签的列表喜欢tag1, tag2, tag3到findByTags方法也不会返回任何结果。例如tag1, tag2返回任何内容等。
我应该如何对这些标签进行搜索?使用一些TextCriteria?
Nei*_*unn 13
这取决于你想提供什么。
如果您只想要一个值,那么 MongoDB 不关心数据是否在数组中,只会在所有条目中查找匹配项
public interface NoteRepository extends MongoRepository<Note, String> {
List<Note> findByTags(String tags);
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一个可变大小的列表来比较可能匹配的“any”,那么spring-mongo 使用了一个影响$in操作的关键字:
public interface NoteRepository extends MongoRepository<Note, String> {
List<Note> findByTagsIn(List<String> tags);
}
Run Code Online (Sandbox Code Playgroud)
所以第一个是你只想"tag1"匹配数据的地方。第二个将匹配一个Listlike "tag1", "tag2",或者任何东西,只要“至少一个”实际上存在于正在搜索的数组中。
| 归档时间: |
|
| 查看次数: |
10508 次 |
| 最近记录: |