My mongo db has documents with _id
defined as string
. Typical values for _id
s are
abc#1234
abc#2345
aby#5678
.abx#123
I want to do a prefix query around abc#
to return abc#1234
abc#2345
(given that _id are sorted in Mongo, I think the prefix query will be efficient --more efficient than 2ndry indexes --Please correct me if I am wrong)
I tried the below and it returned a list of size 0.
@Repository
public interface DBEntityRespository extends MongoRepository<DBEntity, String> {
public List<DBEntity> findByIdStartingWith(String id);
}
entity is defined as...
@Document(collection = "someDBEntity")
@Data
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DBEntity {
@Id
@JsonProperty("_id")
private String id;
...
...
...
}
I am calling the repository as follows
@GetMapping("/v1/x")
public String hi(){
List<DBEntity> list = repo.findByIdStartingWith("abc");// << tried with ^abc , /abc/, abc.* but no luck
System.out.println(list.size() ); // << returns list of size 0
return "success";
}
Any thoughts on how to get the issue fixed? and return the full documents matching the prefix abc
??