MongoDB Panache查询不返回任何结果

MongoDB Panache查询不返回任何结果,mongodb,quarkus,quarkus-panache,Mongodb,Quarkus,Quarkus Panache,我在查询MongoDB时遇到问题。 每当我试图通过ID或任何其他字段查找时,我总是得到零结果。 我在使用“like”操作符时也遇到了问题 我想以不区分大小写的方式查询这些书的标题。我知道你可以在MongoDB中这样做: {title: {$regex: /^query.*/i}} 我试图以华丽的姿态做到这一点,但我无法让它发挥作用: Book.find("title like ?1", "/^" + query + ".*/i"); 我看到控制台上的以下行被打印出来: {'title':{'

我在查询MongoDB时遇到问题。 每当我试图通过ID或任何其他字段查找时,我总是得到零结果。 我在使用“like”操作符时也遇到了问题

我想以不区分大小写的方式查询这些书的标题。我知道你可以在MongoDB中这样做:

{title: {$regex: /^query.*/i}}
我试图以华丽的姿态做到这一点,但我无法让它发挥作用:

Book.find("title like ?1", "/^" + query + ".*/i");
我看到控制台上的以下行被打印出来: {'title':{'$regex':'/^Harr.*/i'}

我还尝试了一个文档,但也没有成功:

Document query = new Document();
query.put("title", "$regex: /^" + query + ".*/i");
Book.find(query);
我得到的结果是零

这是我的图书课:

public class Book extends PanacheMongoEntity {

@NotEmpty
public String title;

@NotEmpty
public String isbn;

@NotEmpty
public String author;

@Min(1)
@NotNull
public BigDecimal price;
}
这是我的图书资源:

@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@Path("/books")
public class BookResource {

@GET
public List<Book> get() {
    return Book.listAll();
}

@GET
@Path("/{id}")
public Book find(@PathParam("id") String id) {
    return (Book) Book.findByIdOptional(id).orElseThrow(() -> new NotFoundException("Book not found"));
}

@GET
@Path("/title/{title}")
public PanacheQuery<PanacheMongoEntityBase> findByTitle(@PathParam("title") String title) {
    Document query = new Document();
//        query.put("title", new BasicDBObject("$regex", ).append("$options", "i"));

    return Optional.ofNullable(Book.find("title like ?1", format("/^%s.*/i", title))).orElseThrow(() -> new NotFoundException("Book not found"));
}

@POST
public void add(@Valid Book book) {
    Book.persist(book);
}

@PUT
@Path("/{id}")
public void update(@PathParam("id") String id, @Valid Book book) {
    Book result = Book.findById(id);
    if (result != null) {
        result.author = book.author;
        result.isbn = book.isbn;
        result.price = book.price;
        result.title = book.title;

        Book.update(result);
    }
}
}
当我试图按id查找一本书时,我也没有得到任何结果:

curl -X GET "http://localhost:8080/books/5eb9475b8a4314145246cc10" -H "accept: application/json"

=> 404 No Books found.
唯一有效的操作是POST(persist),其余的方法不返回任何内容

我有以下设置:

  • MongoDB 4.2通过Docker运行
  • Quarkus 1.4.2
  • JDK 11
这是我的application.properties:

quarkus.mongodb.connection-string=mongodb://localhost:27017
quarkus.mongodb.database=books
quarkus.log.category."io.quarkus.mongodb.panache.runtime".level=DEBUG

以下代码错误:

@GET
@Path("/title/{title}")
public PanacheQuery<PanacheMongoEntityBase> findByTitle(@PathParam("title") String title) {
    Document query = new Document();
    //query.put("title", new BasicDBObject("$regex", ).append("$options", "i"));

    return Optional.ofNullable(Book.find("title like ?1", format("/^%s.*/i", title))).orElseThrow(() -> new NotFoundException("Book not found"));
}

list()
find().list()
的快捷方式,如果您不需要分页或
PanacheQuery
界面上提供的其他功能,可以直接使用
list()

以下代码错误:

@GET
@Path("/title/{title}")
public PanacheQuery<PanacheMongoEntityBase> findByTitle(@PathParam("title") String title) {
    Document query = new Document();
    //query.put("title", new BasicDBObject("$regex", ).append("$options", "i"));

    return Optional.ofNullable(Book.find("title like ?1", format("/^%s.*/i", title))).orElseThrow(() -> new NotFoundException("Book not found"));
}

list()
find().list()
的快捷方式,如果您不需要分页或
PanacheQuery
界面上提供的其他功能,您可以直接使用
list()

好的,我已经将其更改为list()…并且我在控制台上看到以下行:[io.qua.mon.pan.run.mon.mongoooperations]。。。。{'title':{'$regex':'/^harr.*/i'}我仍然没有看到任何结果。好的,我看到发生了什么…正确的查询应该没有引号!如果我这样做,它就会工作:returnbook.list(格式(“{title:{$regex:/.*%s.*/I}”,title));这是一个华丽的错误,它在属性(标题)和它的值上加引号。另一个问题:当我只按ObjectId搜索时,为什么findById也不返回任何结果?在Indead中,Quarkus中当前只支持字符串模式,而不是JavaScript模式(以/开头,以/结尾的模式,可能还有一些选项)。对JavaScript模式的支持目前正在这里讨论:如果这与mongo java驱动程序的行为类似,那么您可以使用“=”和pattern.compile()作为查询来获得正确的结果。可能值得一试。好的,我已将其更改为list()…并且在控制台上看到以下行:[io.qua.mon.pan.run.MongoOperations]。。。。{'title':{'$regex':'/^harr.*/i'}我仍然没有看到任何结果。好的,我看到发生了什么…正确的查询应该没有引号!如果我这样做,它就会工作:returnbook.list(格式(“{title:{$regex:/.*%s.*/I}”,title));这是一个华丽的错误,它在属性(标题)和它的值上加引号。另一个问题:当我只按ObjectId搜索时,为什么findById也不返回任何结果?在Indead中,Quarkus中当前只支持字符串模式,而不是JavaScript模式(以/开头,以/结尾的模式,可能还有一些选项)。对JavaScript模式的支持目前正在这里讨论:如果这与mongo java驱动程序的行为类似,那么您可以使用“=”和pattern.compile()作为查询来获得正确的结果。也许值得一试。
@GET
@Path("/title/{title}")
public List<Book> findByTitle(@PathParam("title") String title) {
    return Book.list("title like ?1", format("/^%s.*/i", title)));
}