1

I'm inserting a Mongo doc with the following time-stamp:

val format = new java.text.SimpleDateFormat("yyyyMMddHHmmss")
format.format(new Date()).toLong

Here's what the section looks like from Mongo's shell:

"{Timestamp" : NumberLong("20130919161948")}"

Based on a few tests, it appears to me that I can simply compare 2 documents by Timestamp by simply checking > or < for the yyyyMMddHHmmss format.

Please let me know if this time-stamp is OK for Mongo. Will I be able to query with it?

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

2 Answers2

2

Mongo will not understand this as a timestamp, but as a number. As you set your date with a format going from year to seconds, you will be able to query mongo using > or < to know if it is before or after.

However if you want to mongo to treat the data as a date, you will need to use the appropriate bson date format. By having mongo treat it as a date, you will have all Mongo date operations available, like extracting year, day of week, etc.. read more

If you are using casbah, and Joda, you can enable serialization and deserialization by an explicit call:

import com.mongodb.casbah.conversions.scala._
RegisterJodaTimeConversionHelpers()

Read more here.

Vinicius Miana
  • 2,047
  • 17
  • 27
  • Looks like there's no `Scala` API (http://bsonspec.org/#/implementation), but there's a `Java` one. So I need to find the right type, and then insert that to Mongo? – Kevin Meredith Sep 20 '13 at 14:31
0

@Kevin, I think you are right. java.util.Date is supported in BSON object.

Using NumberLong to represent timestamp allows you to do range queries, but with BSON date type, date operation in aggregation framework becomes possible, which is more powerful.

visualzhou
  • 381
  • 1
  • 7