JAVA:查询两个时间戳范围内的数据

时间:2019-11-15 09:08:49

标签: java mongodb timestamp

im当前正在尝试从MongoDB中获取数据,其中在查询中使用Timestamp字段进行过滤。 我有这个功能,我想在两个时间戳之间获取我保存的实体的列表。结果始终为0。

为什么我没有任何数据?

Java:12 MongoDB:4.2.0社区

import java.sql.Timestamp;


Timestamp from = Timestamp.valueOf("2015-03-15 00:30:54.160");
Timestamp to = Timestamp.valueOf("2019-11-15 08:00:00.000");

public List<?> getAllLogsFromXToY(Timestamp from, Timestamp to) {

        FindIterable<Document> result
        = getCollection(entity).find(
            and(
                gte("creationDate",from),
                lte("creationDate", to)));

        return parseQueryResult(result, <clazzObj>);
    }
MongoDB Data: 

{
  "_id": "5DCE6CF965D6D0067F4065A7",
  "RECORDS": [
    {
      "newValue": "\\töüwläuq\\",
      "object_type": "de.hfu.revhiso.files.MongoFile",
      "log_category": "ENTITY_OPERATION",
      "description": "changed field filePath",
      "TX_ID__id": "5DCE6CF965D6D0067F4065A7",
      "oldValue": "\\",
      "entity_id": "5DCD6CF6E3420915ABE67B56",
      "modified_date": "6758113787372371968",
      "object_attribute": "filePath",
      "classicication": "USER_OPERATIONS"
    }
  ],
  "USER_ID__id": "5DCD6CD9E3420915ABE5FC2A",
  "creationDate": "6758113787372371968",
  "MODIFIED_USER__id": "5DCD6CD9E3420915ABE5FC2A"
}

@Entity
@Table(name = "t_log_transactions")
@NoSql(dataFormat = DataFormatType.MAPPED)
public class LogTransaction  extends RevhisoEntity  {

    @ManyToOne
    @Field(name = "account_id")
    protected User user_id;  // Ausführende

    @ManyToOne
    @Field(name = "modified_user_id")
    protected User modified_user; //Auftraggeber

    @ElementCollection
    private List<LogRecord> records = new Vector<LogRecord>();

    @Basic
    @Field(name = "creationDate")
    @Temporal(value = TemporalType.TIMESTAMP)
    protected java.util.Date creationDate;

编辑:我使用JPA(eclipseLink)插入数据。我在帖子中添加了实体

1 个答案:

答案 0 :(得分:0)

我找到了答案,为什么它没有显示任何结果...时间戳被保存为prasad_所说的数字或字符串。 问题是,Eclipselink用错误的方式解析了Date。

初始问题的解决方案:

using LocalDateTime for the Date.