Repository: quarkusio/quarkus
Issue: PanacheMongoRepository does not deal well with instants

When using the PanacheMongoRepository to query the database using Instants the Inventory does not find any results unless the query is written in mongo syntax.

`fun getByCreationInstantBefore(time: Instant) = list("endAt < ?1", time)`
 
Should return the Commands present in the database with endAt < time but returns an empty list.

Steps to reproduce the behavior:
1.  Create a Database populated with entries with time
2. Try to query by time like above

`fun getByCreationInstantBefore(time: Instant) = list("{endAt : { \\$lte : ISODate(?1) }}", time)` works well

Changed files:
extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java
extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/CommonQueryBinder.java

diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/CommonQueryBinder.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/CommonQueryBinder.java
index 8ab9230ad6d..f63874a48b4 100644
--- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/CommonQueryBinder.java
+++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/CommonQueryBinder.java
@@ -1,8 +1,10 @@
 package io.quarkus.mongodb.panache.runtime;
 
 import java.text.SimpleDateFormat;
+import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.ZoneOffset;
 import java.time.format.DateTimeFormatter;
 import java.util.Date;
 
@@ -34,6 +36,10 @@ static String escape(Object value) {
             LocalDateTime dateValue = (LocalDateTime) value;
             return "ISODate('" + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(dateValue) + "')";
         }
+        if (value instanceof Instant) {
+            Instant dateValue = (Instant) value;
+            return "ISODate('" + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(dateValue.atZone(ZoneOffset.UTC)) + "')";
+        }
         return "'" + value.toString().replace("\\\\", "\\\\\\\\").replace("'", "\\\\'") + "'";
     }
 }
diff --git a/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java b/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java
index f8dc5d55b1d..b7bab77eba3 100644
--- a/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java
+++ b/extensions/panache/mongodb-panache/runtime/src/test/java/io/quarkus/mongodb/panache/runtime/MongoOperationsTest.java
@@ -5,6 +5,7 @@
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
+import java.time.ZoneOffset;
 import java.util.Date;
 
 import org.bson.codecs.pojo.annotations.BsonProperty;
@@ -35,6 +36,10 @@ public void testBindShorthandQuery() {
         query = MongoOperations.bindQuery(Object.class, "field", new Object[] { LocalDateTime.of(2019, 3, 4, 1, 1, 1) });
         assertEquals("{'field':ISODate('2019-03-04T01:01:01')}", query);
 
+        query = MongoOperations.bindQuery(Object.class, "field",
+                new Object[] { LocalDateTime.of(2019, 3, 4, 1, 1, 1).toInstant(ZoneOffset.UTC) });
+        assertEquals("{'field':ISODate('2019-03-04T01:01:01')}", query);
+
         query = MongoOperations.bindQuery(Object.class, "field",
                 new Object[] { toDate(LocalDateTime.of(2019, 3, 4, 1, 1, 1)) });
         assertEquals("{'field':ISODate('2019-03-04T01:01:01.000')}", query);
@@ -68,6 +73,10 @@ public void testBindNativeQueryByIndex() {
                 new Object[] { LocalDateTime.of(2019, 3, 4, 1, 1, 1) });
         assertEquals("{'field': ISODate('2019-03-04T01:01:01')}", query);
 
+        query = MongoOperations.bindQuery(Object.class, "{'field': ?1}",
+                new Object[] { LocalDateTime.of(2019, 3, 4, 1, 1, 1).toInstant(ZoneOffset.UTC) });
+        assertEquals("{'field': ISODate('2019-03-04T01:01:01')}", query);
+
         query = MongoOperations.bindQuery(Object.class, "{'field': ?1}",
                 new Object[] { toDate(LocalDateTime.of(2019, 3, 4, 1, 1, 1)) });
         assertEquals("{'field': ISODate('2019-03-04T01:01:01.000')}", query);
@@ -99,6 +108,10 @@ public void testBindNativeQueryByName() {
                 Parameters.with("field", LocalDateTime.of(2019, 3, 4, 1, 1, 1)).map());
         assertEquals("{'field': ISODate('2019-03-04T01:01:01')}", query);
 
+        query = MongoOperations.bindQuery(Object.class, "{'field': :field}",
+                Parameters.with("field", LocalDateTime.of(2019, 3, 4, 1, 1, 1).toInstant(ZoneOffset.UTC)).map());
+        assertEquals("{'field': ISODate('2019-03-04T01:01:01')}", query);
+
         query = MongoOperations.bindQuery(Object.class, "{'field': :field}",
                 Parameters.with("field", toDate(LocalDateTime.of(2019, 3, 4, 1, 1, 1))).map());
         assertEquals("{'field': ISODate('2019-03-04T01:01:01.000')}", query);
@@ -127,6 +140,10 @@ public void testBindEnhancedQueryByIndex() {
         query = MongoOperations.bindQuery(Object.class, "field = ?1", new Object[] { LocalDateTime.of(2019, 3, 4, 1, 1, 1) });
         assertEquals("{'field':ISODate('2019-03-04T01:01:01')}", query);
 
+        query = MongoOperations.bindQuery(Object.class, "field = ?1",
+                new Object[] { LocalDateTime.of(2019, 3, 4, 1, 1, 1).toInstant(ZoneOffset.UTC) });
+        assertEquals("{'field':ISODate('2019-03-04T01:01:01')}", query);
+
         query = MongoOperations.bindQuery(Object.class, "field = ?1",
                 new Object[] { toDate(LocalDateTime.of(2019, 3, 4, 1, 1, 1)) });
         assertEquals("{'field':ISODate('2019-03-04T01:01:01.000')}", query);
@@ -180,6 +197,10 @@ public void testBindEnhancedQueryByName() {
                 Parameters.with("field", LocalDateTime.of(2019, 3, 4, 1, 1, 1)).map());
         assertEquals("{'field':ISODate('2019-03-04T01:01:01')}", query);
 
+        query = MongoOperations.bindQuery(Object.class, "field = :field",
+                Parameters.with("field", LocalDateTime.of(2019, 3, 4, 1, 1, 1).toInstant(ZoneOffset.UTC)).map());
+        assertEquals("{'field':ISODate('2019-03-04T01:01:01')}", query);
+
         query = MongoOperations.bindQuery(Object.class, "field = :field",
                 Parameters.with("field", toDate(LocalDateTime.of(2019, 3, 4, 1, 1, 1))).map());
         assertEquals("{'field':ISODate('2019-03-04T01:01:01.000')}", query);