question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Panache Query With Mongo: and operator not working properly with multiple conditions on same field

See original GitHub issue

The following test is failing:

    @Test
    public void failingQuery() {

        UUID videoId = UUID.randomUUID();
        LocalDateTime now = LocalDateTime.now();
        View view = new View(videoId, now.minusMinutes(10));
        view.persist();

        long count1 = View.count("createdAt > ?1", now.minusMinutes(2));
        logger.info("count 1: {}", count1);
        assertThat(count1).isEqualTo(0); // passing

        long count2 = View.count("createdAt > ?1 and createdAt < ?2", now.minusMinutes(2), now);
        logger.info("count 2: {}", count2);
        assertThat(count2).isEqualTo(0); // failing (count2 is equal to 1)
    }

I think the corresponding mongo query should be

    createdAt: {
        $gte: ISODate(date1),
        $lt: ISODate(date2)
    }

The actual mongo query created by panache is:

{
    'createdAt':{'$gt':ISODate('2020-09-14T18:49:36.897Z')},
    'createdAt':{'$lt':ISODate('2020-09-14T18:51:36.897Z')}
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
loicmathieucommented, Sep 16, 2020

@rmanibus so there is an issue, a query must be a valid json document, so if you use multiple condition on a query it must be defined as a sub document (and not defined as multiple times the same field as it is done today).

Today we use a string based query generator, we may need to revisit this to fix this issue.

To workaround this, you can use a native query untill we provide a proper fix:

View.count("{'createdAt': {'$gt':?1, '$lt': ?2}}", now.minusMinutes(2), now);
0reactions
ajsaraujocommented, Dec 6, 2022

A fix for this would really come in handy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simplified MongoDB with Panache - Quarkus
MongoDB queries must be valid JSON documents, using the same field multiple times in a query is not allowed using PanacheQL as it...
Read more >
MongoDB query with multiple conditions - Stack Overflow
Above query work as "OR" condition. I need "AND". i.e. Result only return on exact match of both condition. ... -1 db.getCollection('TheCollection').find({ ' ......
Read more >
Update Multiple Fields in a MongoDB Document - Baeldung
In this tutorial, we'll learn to modify the multiple fields of a document using the update and the replace query. For demonstration purposes ......
Read more >
$and — MongoDB Manual
AND Queries With Multiple Expressions Specifying the Same Operator ... The query selects all documents where: ... The query cannot use an implicit...
Read more >
Simplified MongoDB with Panache - Quarkus
Normally, MongoDB queries are of this form: {'firstname': 'John', 'lastname':'Doe'} , this is what we call MongoDB native queries. You can use them...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found