5 kyu

Mongodb ObjectID

640 of 1,551user9027523

Description:

MongoDB is a noSQL database which uses the concept of a document, rather than a table as in SQL. Its popularity is growing.

As in any database, you can create, read, update, and delete elements. But in constrast to SQL, when you create an element, a new field _id is created. This field is unique and contains some information about the time the element was created, id of the process that created it, and so on. More information can be found in the MongoDB documentation (which you have to read in order to implement this Kata).

So let us implement the following helper which will have 2 methods:

  • one which verifies that the string is a valid Mongo ID string, and
  • one which finds the timestamp from a MongoID string

Note:

If you take a close look at a Codewars URL, you will notice each kata's id (the XXX in http://www.codewars.com/dojo/katas/XXX/train/javascript) is really similar to MongoDB's ids, which brings us to the conjecture that this is the database powering Codewars.

Examples

The verification method will return true if an element provided is a valid Mongo string and false otherwise:

Mongo.isValid('507f1f77bcf86cd799439011')  // true
Mongo.isValid('507f1f77bcf86cz799439011')  // false
Mongo.isValid('507f1f77bcf86cd79943901')   // false
Mongo.isValid('111111111111111111111111')  // true
Mongo.isValid(111111111111111111111111)    // false
Mongo.isValid('507f1f77bcf86cD799439011')  // false (we arbitrarily only allow lowercase letters)
Mongo.is_valid('507f1f77bcf86cd799439011')  # True
Mongo.is_valid('507f1f77bcf86cz799439011')  # False
Mongo.is_valid('507f1f77bcf86cd79943901')   # False
Mongo.is_valid('111111111111111111111111')  # True
Mongo.is_valid(111111111111111111111111)    # False
Mongo.is_valid('507f1f77bcf86cD799439011')  # False (we arbitrarily only allow lowercase letters)
Mongo.is_valid('507f1f77bcf86cd799439011')  # true
Mongo.is_valid('507f1f77bcf86cz799439011')  # false
Mongo.is_valid('507f1f77bcf86cd79943901')   # false
Mongo.is_valid('111111111111111111111111')  # true
Mongo.is_valid(111111111111111111111111)    # false
Mongo.is_valid('507f1f77bcf86cD799439011')  # false (we arbitrarily only allow lowercase letters)

The timestamp method will return a date/time object from the timestamp of the Mongo string and false otherwise:

// Mongo.getTimestamp should return a Date object

Mongo.getTimestamp('507f1f77bcf86cd799439011')  // Wed Oct 17 2012 21:13:27 GMT-0700 (Pacific Daylight Time)
Mongo.getTimestamp('507f1f77bcf86cz799439011')  // false
Mongo.getTimestamp('507f1f77bcf86cd79943901')   // false
Mongo.getTimestamp('111111111111111111111111')  // Sun Jan 28 1979 00:25:53 GMT-0800 (Pacific Standard Time)
Mongo.getTimestamp(111111111111111111111111)    // false
# Mongo.get_timestamp should return a datetime object

Mongo.get_timestamp('507f1f77bcf86cd799439011')  # Wed Oct 17 2012 21:13:27 GMT-0700 (Pacific Daylight Time)
Mongo.get_timestamp('507f1f77bcf86cz799439011')  # False
Mongo.get_timestamp('507f1f77bcf86cd79943901')   # False
Mongo.get_timestamp('111111111111111111111111')  # Sun Jan 28 1979 00:25:53 GMT-0800 (Pacific Standard Time)
Mongo.get_timestamp(111111111111111111111111)    # False
# Mongo.get_timestamp should return a datetime object

Mongo.get_timestamp('507f1f77bcf86cd799439011')  # Wed Oct 17 2012 21:13:27 GMT-0700 (Pacific Daylight Time)
Mongo.get_timestamp('507f1f77bcf86cz799439011')  # false
Mongo.get_timestamp('507f1f77bcf86cd79943901')   # false
Mongo.get_timestamp('111111111111111111111111')  # Sun Jan 28 1979 00:25:53 GMT-0800 (Pacific Standard Time)
Mongo.get_timestamp(111111111111111111111111)    # false

When you will implement this correctly, you will not only get some points, but also would be able to check creation time of all the kata here :-)

MongoDB
Regular Expressions
Strings
Fundamentals

Stats:

CreatedFeb 15, 2014
PublishedFeb 15, 2014
Warriors Trained6039
Total Skips2032
Total Code Submissions20040
Total Times Completed1551
JavaScript Completions640
Python Completions890
Ruby Completions54
Total Stars181
% of votes with a positive feedback rating85% of 231
Total "Very Satisfied" Votes173
Total "Somewhat Satisfied" Votes46
Total "Not Satisfied" Votes12
Ad
Contributors
  • user9027523 Avatar
  • jhoffner Avatar
  • laoris Avatar
  • anter69 Avatar
  • user8436785 Avatar
  • Just4FunCoder Avatar
Ad