简体   繁体   English

如何从Java在Mongo DB中插入日期

[英]How to insert date in mongo db from java

There are many similar questions asked. 有很多类似的问题要问。 But not exactly similar to the issue i am facing. 但与我面临的问题并不完全相似。 I have seen almost all the questions and answers around it 我已经看到了几乎所有的问题和答案

So the problem is 所以问题是

I got to insert a date field in my mongo collection 我必须在mongo集合中插入一个日期字段

But I can't access the collection directly. 但是我无法直接访问该集合。 I got to use a service. 我必须使用一项服务。 The service takes a string and returns me oid. 该服务需要一个字符串并返回我的oid。

So once i construct the BasicDBObject I call toString on it and pass it on to my service.. I even tried inserting it directly in a test collection and mongo is complaining. 因此,一旦我构造BasicDBObject,便在其上调用toString并将其传递给我的服务。。我什至尝试将其直接插入测试集合中,而mongo则抱怨。

 BasicDBObject document = new BasicDBObject(); 
    long createdAtSinceEpoch = 0;
    long expiresAtSinceEpoch = 0;

    createdAtSinceEpoch = System.nanoTime();
    Date createdAt = new Date(TimeUnit.NANOSECONDS.toMillis(createdAtSinceEpoch));
    document.append("createdAt", createdAt);

    expiresAtSinceEpoch = createdAtSinceEpoch + +TimeUnit.SECONDS.toNanos(30);
    Date expiresAt = new Date(TimeUnit.NANOSECONDS.toMillis(expiresAtSinceEpoch));
document.append("expiresAt", expiresAt);
service.storeRecord(document.toString());

and the generated JSON String looks like 生成的JSON字符串看起来像

{
"createdAt": {
    "$date": "2015-09-01T20:05:21.641Z"
},
"expiresAt": {
    "$date": "2015-09-01T20:05:51.641Z"
}

and Mongo complains that 蒙哥抱怨

Unable to parse JSON : Date expecting integer milliseconds, at (3,17)

So If i pass milliseconds alone instead of date object in the document.append() method then it DOES NOT recognize this field as date and considers it as String but inserts into the collection 因此,如果我单独传递毫秒(而不是document.append()方法中的date对象),则它不会将此字段识别为日期,而是将其视为String,但会插入到集合中

I need 2 things 我需要两件事

1) I want the data to be inserted 2) I am planning to expire that row by adding an index to the expiresAt field. 1)我希望插入数据2)我计划通过在expiresAt字段中添加索引来使该行过期。 So I want mongo to recognize that its a date field 所以我想让mongo认识到它的日期字段

JSON makes a difference between a numeric field and a text field containing a number. JSON使数字字段和包含数字的文本字段有所不同。 The latter one is only recognized as a String ; 后者仅被识别为String ; I assume that this is what you did when you thought you were giving your service the date as an integer. 我认为这就是您以为服务日期为整数时所做的事情。 Unfortunately you didn't show us the relevant code. 不幸的是,您没有向我们显示相关代码。

When I save the Date info as a non String format, I annotate the field in my DTO as below. 当我将日期信息另存为非字符串格式时,我在DTO中对该字段进行了注释,如下所示。 This helps the MongoDB know that the field is to be treated as an ISO date which then would be useful for making range search etc., 这有助于MongoDB知道该字段将被视为ISO日期,这对于进行范围搜索等非常有用,

@DateTimeFormat(iso = ISO.DATE_TIME) private Date date;
Date date = new Date();

BasicDBObject date= new BasicDBObject("date", date);
Data.insert(date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM