1

currently, I'm developing a Java/Spring App where I save and provide JSON from/to clients.

I've stores my Date information as a Unix timestamp in a millisecond. Now MongoDB stores this automatically as

"StartTime": {"$numberLong": "1549640550000"}

when I insert

"StartTime": 1549640550000

I've also read that this happens with $Date instead of $numberLong when I use the iso8601 format.

Is it possible to read the Time information from MongoDB without this Wrapper Information in my query result?

It seems pretty uncomfortable to me to handle/remove the information te receive the plain TimeStamp Information.

EDIT: When I use document.toJSON ( I know it's deprecated) I get

"StartTime": {"$numberLong": "1549640550000"}

when I use document.toString() I get TimeStamp=Document{StartTime=1549554150000}

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51
  • If you convert long timestamp to string, would that work for you? – M. Prokhorov Jul 30 '19 at 13:33
  • I can try but is that a good practice? Thing is, I use regex to filter data for a specific time intervall given by the user. So opertaions like $gt und $lt are necessary to work – FishingIsLife Jul 30 '19 at 13:43
  • 1
    If we're talking about good practice, then I think it's best to store dates as actual dates: https://docs.mongodb.com/manual/reference/method/Date/ and also this: https://stackoverflow.com/questions/3778428/best-way-to-store-date-time-in-mongodb – M. Prokhorov Jul 30 '19 at 13:48
  • That is the way I will actually go. As far as I know Date String ( iso String I mentiond in my question) are also compareable. But when I get the JSOn back I guess there might be $Date: instead of $numberLong in front of the time information. – FishingIsLife Jul 30 '19 at 13:52
  • That thing is supposed to be handled by driver to convert into concrete types when reading or writing, because Mongo internally just stores in text files, it seems. Try as it's suggested in that q'n'a I linked first, there might be no problems with it. – M. Prokhorov Jul 30 '19 at 13:55
  • Yes I will try this solution, thanks. I don't use pojos or stuff like that I just want to store and read the jsons. It's pretty confusing for me that it is not possible to read Documents without the Mongo specific helper information. – FishingIsLife Jul 30 '19 at 13:58
  • This information is needed for Mongo to keep at least some form of schema for documents - since it's just the arbitrary JSON which is stored in text form, you wouldn't be able to write a date and get a date back (you'd always get a string, even if writing a date). Other DBMS (SQL ones specifically) get around this by having the schema be strict, external from data and bound to table. Mongo, having declared that it won't have a strict schema, had to put schema in values themselves. (It's my thoughts on this, I wouldn't know if they *had* to do it) – M. Prokhorov Jul 30 '19 at 14:15

2 Answers2

1

You need to build a JSON serializer which can convert NumberLong to Long. You can use the following settings and pass in Document.toJson() as a parameter and get the expected output.

JsonWriterSettings settings = JsonWriterSettings.builder()
            .int64Converter((value, writer) -> writer.writeNumber(value.toString())).build();

document.toJson(settings); // The output would contain simple long value
Himanshu Sharma
  • 2,940
  • 1
  • 7
  • 18
0

This one works for me. I figure it out after spending hours on the internet. You can try any one of them.

# These 2 queries are equivalent
db.looks.find({ $and: [{ "version":"v2" }, { "styleid":NumberLong("123456789") }]}).count()
db.looks.find({ "$and": [{ "version":"v2" }, { "styleid":{"$eq":123456789} }]}).count()
db.looks.find({ "$and": [{ "version":"v2" }, { "styleid":{"$in":[123456789]} }]}).count() 

# Java Code 
Criteria criteria = new Criteria();
criteria.andOperator(
            Criteria.where(Constants.VERSION).is(Constants.V2),
            Criteria.where(Constants.STYLEID).in(Lists.newArrayList(looksRequestV2.getStyleId().intValue()))
            // Criteria.where(Constants.STYLEID).is(looksRequestV2.getStyleId().intValue())
            // Criteria.where(Constants.STYLEID).is(looksRequestV2.getStyleId())
);
roottraveller
  • 7,942
  • 7
  • 60
  • 65