Logo

dev-resources.site

for different kinds of informations.

Converting MongoDB ObjectId to Timestamp in Snowflake: A Friendly Guide

Published at
11/24/2023
Categories
mongodb
snowflak
db
nosql
Author
polakshahar
Categories
4 categories in total
mongodb
open
snowflak
open
db
open
nosql
open
Author
11 person written this
polakshahar
open
Converting MongoDB ObjectId to Timestamp in Snowflake: A Friendly Guide

Introduction

Hey there, fellow developers! Today, let's unravel a nifty trick that often stumps many of us when working with MongoDB and Snowflake.

Picture this: You've got data in MongoDB, each record stamped with that unique ObjectId MongoDB is famous for. Now, you need to migrate this data into Snowflake, but wait โ€“ how do you make sense of those timestamps hidden inside those ObjectIds? Fear not!

In this post, I'll walk you through a simple yet effective solution to extract those timestamps and bring them into the light in Snowflake.


The Challenge Explained

MongoDB's ObjectId isn't just a random jumble of characters; it's a treasure trove of information. The first 8 characters are actually a timestamp, representing when the data was created.

The catch? It's in hexadecimal format. And here's where Snowflake throws us a curveball โ€“ it doesn't have a built-in function to convert hexadecimal to decimal. So, how do we crack this code? Let's find out!


Step-by-Step Solution

Step 1: The Goal

  • Objective: Extract the timestamp from MongoDB's ObjectId in Snowflake.
  • The Hurdle: Snowflake's SQL doesn't directly convert hex to decimal.

Image depicts a data table with four columns and four rows. The first column header is '_id' indicating unique identifiers for records. The second column header is 'current_version' showing version numbers . The third column is 'model_id' . On the right side of the image, there is a  text 'CREATED_AT' with a shrugging emoji above it, suggesting the addition of a new 'created_at' column to indicate record creation times. The overall image conveys the process of enhancing a database with time-related data.


Step 2: Our Secret Weapon โ€“ A Custom JavaScript UDF

  • UDFs to the Rescue: In Snowflake, we can create User-Defined Functions (UDFs) to perform custom operations. We'll use JavaScript for our UDF.
  • UDF Creation Script: Here's how we set up our UDF.

    CREATE OR REPLACE FUNCTION hex_to_dec(hex_str STRING)
    RETURNS FLOAT
    LANGUAGE JAVASCRIPT
    AS $$
        return parseInt(HEX_STR, 16);
    $$;
    
  • What's Happening Here?: This snippet tells Snowflake, "Hey, let's turn those hex values into something we can work with in decimal!"


Step 3: Putting Our UDF to Work

  • The Magic Query: Now, we use our UDF in a SQL query.

    CREATE OR REPLACE TABLE MONGO_IMPORT_WITH_TIMESTAMP AS
    SELECT
        *,
        TO_TIMESTAMP_NTZ(CAST(hex_to_dec(SUBSTR(_id, 1, 8)) AS INTEGER)) AS created_at
    FROM
        MONGO_IMPORT;
    
  • Breaking It Down: This query is our secret sauce. It says, "Let's create a new table, MONGO_IMPORT_WITH_TIMESTAMP, where we'll turn those hex timestamps into human-friendly dates."


Step 4: Test and Celebrate

  • Always Test: Before we break out the confetti, let's test this with some sample data.
  • What to Expect: If all goes well, you'll have a shiny new table in Snowflake, with all your MongoDB timestamps now readable and ready to use.

Wrapping Up

In wrapping up, let's recall the words of W. Edwards Deming:

'Without data, you're just another person with an opinion.'

Our journey through converting MongoDB ObjectIds to readable timestamps in Snowflake is a testament to this truth. By unlocking the data hidden within these identifiers, we turn mere numbers into meaningful insights.


Helpful Resources

db Article's
30 articles in total
Favicon
๐Ÿ› ๏ธ DB Migration For Golang Services, Why it matters? ๐Ÿน
Favicon
MongoDb Atlas: manual backup and restore data
Favicon
Import the database from the Heroku dump
Favicon
Why MongoDB? Exploring the Benefits and Use Cases of a Leading NoSQL Database
Favicon
Database Pool Management with Sidekiq and load_async
Favicon
HyperGraphs In Relation Model
Favicon
ACID in DB
Favicon
Use EXISTS instead of COUNT > 0 when checking if records exist
Favicon
Converting MongoDB ObjectId to Timestamp in Snowflake: A Friendly Guide
Favicon
Level UP your RDBMS Productivity in GO
Favicon
Iniciar sesion en mongodb con usuario y contraseรฑa | Mongodb
Favicon
DB POOL
Favicon
Exploring String Field Types in Databases: CHAR, VARCHAR, and TEXT
Favicon
How to connect to MySQL DB
Favicon
How to provision an AWS RDS MySQL Database
Favicon
Amazon RDS for MySQL
Favicon
Amazon Aurora
Favicon
Amazon RDS
Favicon
Understanding the Differences Between SQL and NoSQL Databases
Favicon
Advantages of Relational DB, Graph DB, and RGDB
Favicon
Understanding Foreign Keys in PostgreSQL
Favicon
What's behind scenes of PostgreSQL from Apache AGE?
Favicon
Spring Boot 3 with H2 database example
Favicon
Basic MongoDB Commands
Favicon
Guide to Apache AGE 1.3.0 Release for PostgreSQL 13
Favicon
Laravel useful DB/Eloquent functions:
Favicon
Docker/DB - Isn't it better to set share directory between host and db container?
Favicon
Alojar Wordpress en la nube (Google Cloud)
Favicon
IceFireDB๏ผšA database that supports IPFS storage and Redis protocol, interesting direction.
Favicon
MongoDB Complete Backup And Restore Command With Atlas

Featured ones: