knguyen
knguyen

Reputation: 3074

Is it a good idea to date as _id when storing time-series data?

I'm new to MongoDB. I'm writing a python script to scrape and update stock quotes data. The script will run once to scrape and build a database up to the latest and then run everyday for updating.

After some researching, I think MongoDb fits the bill. Currently, I'm setting up the date as '_id' because I want to ensure uniqueness (since the update also scrapes from a page containing data from previous days).

Is it a potential disastrous idea? If so, how should I do otherwise? Thanks

Upvotes: 1

Views: 69

Answers (1)

Roberto
Roberto

Reputation: 9080

No, It's not a good idea, because, by default, MongoDB already saves the timestamp in the _id:

You can retrieve the _id data using this code:

date = new Date( parseInt( _id.toString().substring(0,8), 16 ) * 1000 )

I'd use the auto-generated MongoDB _id

EDIT: (Brought from comments) If you are using PyMongo, the objectid python object has the attribute generation_timefrom which you can extract the related datetime.datetime instance . PyMongo API Doc

>>> ObjectId().generation_time

Upvotes: 1

Related Questions