logo
Inery

1 year ago

How To Transfer MongoDB Data Into IneryDB

article_image

See more news

news_image
How Decentralization Empowers Digital Freedom: The Data Rebellion
news_image
Why Minimalist Data Management Leads to Better Performance

The tech undercurrents of MongoDB and IneryDB are quite different. MongoDB is a document-based NoSQL system, while IneryDB works on the proprietary Inery Blockchain. As such, migrating from the former to the latter sounds challenging. But fear not: the process isn’t nearly as convoluted as you may have imagined. 

This streamlined guide will show you the most important steps for transferring from MongoDB to IneryDB.

Before You Get Started

While migrating data from MongoDB to IneryDB, it’s important to keep three things in mind:

    • The data format in Inery Blockchain tables is more structured, so do some planning and data modeling before starting
    • Generally, data in Inery Blockchain tables is more compressed to reduce required storage space
    • Inery blockchain is immutable, so double-check if the appropriate data types are in the corresponding fields

Schema Design

MongoDB and IneryDB documents have nearly identical row structures. So, we can observe both like a row in a table.

{"_id":{"$oid":"5c8eccc1caa187d17ca70801"},"city":"MILLER","zip":"62962","loc":

{"y":37.103359,"x":89.349443},"pop":122,"state":"IL"}

After mapping the data from MongoDB to the Inery Blockchain table, you need to pick the appropriate data types for the fields. This is vital for making sure the data gets recorded on the blockchain correctly. 

Once you decide on the data type, you can create a value contract table for the database. The value contract defines the structure and validation rules for the data on the blockchain. 

Important note: test the value contract table thoroughly before you deploy it on the live blockchain!

#include <inery/inery.hpp>

...

class [[inery::contract("postalCodes")]] postalCodes : public contract {

public:

using contract::contract;

struct [[inery::table("zip")]] zip

{

uint64_t id;

string _id;

string city;

uint32_t zip;

//note that we used sstd::pair for our loc field

std::pair<double, double>   loc;

uint32_t pop;

string state;

uint64_t primary_key() const { return id; }

};

typedef inery::multi_index<"zip"_n, zip> zip_inst;

Exporting Data from MongoDB

Now, we can export the data you have on MongoDB. Let’s say the below is the data you need to export:

Perhaps the easiest way to go about this is using mongoexport. Mongoexport is a command-line tool for exporting MongoDB data to a file.

The exported data needs to be in one of two formats: JSON or CSV. You have to specify the collection you want to export and the output file location.

This example is for a JSON export:

mongoexport --db exampleDB --collection exampleCollection --out path/example.json

You can use the command to see if the file exists, and the output should look similar to this:

Once the data has been exported, access the data to check if it’s been exported and formatted properly. The handiest way to do this is through nano file.json:

Be mindful of the fact that MongoDB doesn’t export data with the right parentheses and commas!

 

Here’s a simple sample of a python3 script for that:

import json

def formatJSON(path):

message = "["

with open(path) as file_to_format:

data = file_to_format.read()

data = data.split("\n") 

for el in data:

message += el

message += ",\n”

message = message[:len(message) - 4]

message += "]"

 

with open(path, 'w') as file_to_format:

file_to_format.write(message)

if __name__ == '__main__':

# 'mongodb-sample-dataset/sample_training/zips.json'

path = input('Enter file path')

formatJSON(path)

 

Creating a Value Contract

Before you make a value contract and transfer data from MongoDB to Inery Blockchain, check the following:

    • The data in MongoDB is correct and updated
    • The data in MongoDB is easily mappable to the Inery Blockchain model
    • The data transfer process has been tested before going live
    • Potential security/privacy risks for the transfer have been accounted for
    • You have a plan for handling errors during the transfer

If you’ve covered all that, you should then decide what data types you’ll be using for the Contract. You should also define which actions the contract allows. 

Below is an example of a contract that has 4 actions: 

    • Insert zip to the table
    • Update zip
    • Delete zip
    • Clear the whole Inery Blockchain table

#include <inery/inery.hpp>

#include

using namespace inery;

using namespace std;

class [[inery::contract("postalCodes")]] postalCodes : public contract {

public:

using contract::contract;

struct [[inery::table("zip")]] zip

{

uint64_t id;

string _id;

string city;

uint32_t zip;

std::pair<double, double> loc;

uint32_t pop;

string state;

uint64_t primary_key() const { return id; }

};

typedef inery::multi_index<"zip"_n, zip> zip_inst;

[[inery::action]] void insertzip(string _id, string city, uint32_t zip, double x,double y, uint32_t pop, string state){

require_auth(get_self());

zip_inst zip_table(get_self(), name("master.prime").value);

zip_table.emplace(get_self(), [&](auto &zipp){

zipp.id = zip_table.available_primary_key();

zipp._id = _id;

zipp.city = city;

zipp.zip = zip;

zipp.loc = std::make_pair(x, y);

zipp.pop = pop;

zipp.state = state;

});

}

[[inery::action]] void updatezip(uint64_t id, string _id, string city, uint32_t zip, double x, double y, uint32_t pop, string state){

require_auth(get_self()); 

zip_inst zip_table(get_self(), name("master.prime").value);

auto zip_to_update = zip_table.find(id);

zip_table.modify(zip_to_update ,get_self(), [&](auto &zipp){

zipp._id = _id;

zipp.city = city;

zipp.zip = zip;

zipp.loc = std::make_pair(x, y);

zipp.pop = pop;

zipp.state = state;

});

}

[[inery::action]] void clearszips(){

require_auth(get_self());

zip_inst zip_table(get_self(), name("master.prime").value);

vector< zip > rmv_zips;

uint64_t dist = distance(zip_table.begin(), zip_table.end());

rmv_zips.reserve(dist);

for(auto zip_it = zip_table.begin(); zip_it != zip_table.end(); zip_it++)

rmv_zips.emplace_back(zip{

.id = zip_it->id,

._id = zip_it->_id,

.city = zip_it->city,

.zip = zip_it->zip,

.loc = zip_it->loc,

.pop = zip_it->pop,

.state = zip_it->state

});

for(auto zip_it = rmv_zips.begin(); zip_it != rmv_zips.end(); zip_it++){

auto rmv_zip_it = zip_table.find(zip_it->id);

if(rmv_zip_it != zip_table.end())

zip_table.erase(rmv_zip_it);

}

}

[[inery::action]] void erasezip(uint64_t id){

require_auth(get_self());

 zip_inst zip_table(get_self(), name("master.prime").value);

auto zip_to_remove = zip_table.find(id);

if(zip_to_remove != zip_table.end())

zip_table.erase(zip_to_remove);

}

};

Calling insertzip action

We can push insertzip by using cline push action ‘[]’ -p @active

Insertzip action inserts data into our zip table on the value contract:

Here’s a sample of the data in the zip table:

{

"rows": [{

"id": 0,

"_id": "5c8eccc1caa187d17ca76050",

"city": "AFTON",

"zip": 83110,

"loc": {

"first": "42.71282899999999927",

"second": "110.94197599999999682”

},

"pop": 3201,

"state": "WY"

},{

"id": 1,

"_id": "5c8eccc1caa187d17ca7604e",

"city": "LYMAN",

"zip": 83111,

"loc": {

"first": "60.73282900000000240",

"second": "113.94197599999999682"

},

"pop": 3301,

"state": "NY”

},{

"id": 2,

"_id": "5c8eccc1caa187d17ca7604d",

"city": "THAYNE",

"zip": 83000,

"loc": {

"first": "70.73282899999999529",

"second": "30.94197600000000037"

},

"pop": 3000,

"state": "AL"

},{

"id": 3,

"_id": "5c8eccc1caa187d17ca7604d",

"city": "THAYNE",

"zip": 83000,

"loc": {

"first": "70.73282899999999529",

"second": "30.94197600000000037”

},

"pop": 3000,

"state": "AL"

}

],

"more": false,

"next_key": "”

}

Calling updatezip action

updatezip action updates data in a table, but we need to pass id as an argument:

Calling deletezip action

deletezip action deletes data in a table, but we need to pass id as an argument:

Calling clearzips action

clearzips action clear the whole database:

Seize the Power of IneryDB: Simplify Your MongoDB Data Migration

IneryDB offers a straightforward solution for transferring MongoDB data, bridging the gap between two seemingly different tech worlds. Before you dive in, remember to plan for structured data, efficient storage, and data type alignment.

With streamlined schema design and effortless data export, the transition becomes a manageable task. Ensure your data is accurate, your mapping aligns, and you're prepared for any challenges along the way.

Embrace IneryDB to simplify your data migration journey and unlock the potential of secure and efficient data management. Take the leap with IneryDB today!

logo
Inery

1 year ago

Google, Amazon, Apple, and Meta: Guardians or Intruders of Privacy?

The biggest names in Big Tech know more about you than you may be comfortable with. Click here to see how bad the situation is. ...READ MORE

artilce_image

Share

logo
Inery

3 weeks ago

Distributed Ledger Technology (DLT): Definition and How It Works

From trust to transparency, Distributed Ledger Technology (DLT) offers industries a secure, collaborative way to manage data without relying on intermediaries. ...READ MORE

artilce_image

Share

logo
Inery

7 months ago

Empowering Smart Cities with Inery's Data Solutions

Discover how Inery can revolutionize urban living by enhancing sustainability, fostering innovation, and protecting data privacy. ...READ MORE

artilce_image

Share

logo
Inery

8 months ago

Quantum Challenges in Blockchain: Where Does Inery Stand?

Learn about Inery's strategic response to quantum computing's impact on blockchain, highlighting the shift towards innovative security measures in this rapidly evolving tech landscape. ...READ MORE

artilce_image

Share

bgbg