当前位置: 首页 > 知识库问答 >
问题:

MongoDB Panache查询未返回任何结果

戎劲
2023-03-14

我在使用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": "5eb9475b8a4314145246cc10",
"author": "J.K. Rowling",
"isbn": "12345678",
"price": 24.95,
"title": "Harry Potter 1"
},
{
"id": "5eb95a758a4314145246cc25",
"isbn": "456",
"price": 0,
"title": "Java for dummies"
},
{
"id": "5eb95b1a8a4314145246cc27",
"author": "steven king",
"isbn": "456",
"price": 10,
"title": "IT"
}
]
curl -X GET "http://localhost:8080/books/5eb9475b8a4314145246cc10" -H "accept: application/json"

=> 404 No Books found.

我有以下设置:

  • 通过Docker运行MongoDB 4.2
  • 夸克1.4.2
  • JDK 11

下面是我的应用程序.属性:

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

共有1个答案

南门正祥
2023-03-14

下面这段代码是错误的:

@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"));
}

book.find()返回一个PanacheQuery,您需要调用查询的终止符操作之一来获得结果:list/stream/findfirst/findone。

我建议这样执行:

@GET
@Path("/title/{title}")
public List<Book> findByTitle(@PathParam("title") String title) {
    return Book.list("title like ?1", format("/^%s.*/i", title)));
}

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

 类似资料:
  • 问题内容: 我正在尝试向表中插入一些行…我正在使用 postgressql-7.2.jar。 我得到以下异常 org.postgresql.util.PSQLException:查询未返回任何结果。 在org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255) 我已经用Googl

  • 问题内容: 这是我的架构 我做了这样的一个NEST查询: 并返回零元素! 如果我查看数据库(使用邮递员),则可以看到我的文档: 问题答案: 听起来您可能没有在为文档建立索引 之前 将类型的映射显式地放入索引中,所以Elasticsearch已基于所看到文档中字段的默认映射来推断该映射。例如,给定以下类型 如果我们按以下方式索引一些文档 在控制台中输出以下内容 我们没有匹配的文件。在Elastics

  • [root@externalsystem~]#curl-xpost-h'请求:{“request”:“prepareandexecute”,“connectionid”:“000000-0000-0000-00000000”,“stateMentID”:12345,“sql”:“select*FROM questtweets1”,“maxrowcount”:1}'http://here.comes

  • 我正在尝试在Firebase中进行简单搜索以从我的Firebase数据中返回数据,但没有任何返回。 JSON结构 我的Firebase搜索代码如下 但一旦代码被执行,它就会返回null。还有一种方法可以搜索特定字段。

  • 我正在尝试运行一个in查询(springboot jpa mysql)。我已经启用了调试日志,查询似乎很好,但是SpringJPA没有返回任何结果。 以下是配置: 这是我的存储库: //或 这里需要注意的是,“in”查询中的列不是主键。 生成的查询:

  • 问题内容: 我正在使用Newtonsoft的Json.Net从以下json中选择节点: 以下C#代码段 产量: 现在,这很酷,我想做的是按客户端代码过滤,我认为 可以,但是我显然对语法不够了解。这将返回一个空列表: 并且单个令牌选择器返回null: 我在https://jsonpath.curiousconcept.com/上尝试了几种不同的配置,看来我的查询语法确实坏了。 使用Flow Comm