How to load SQL data into MongoDB using python script

All other Source.Python topics and issues.
Ram668000
Junior Member
Posts: 6
Joined: Fri Sep 25, 2020 7:29 am

How to load SQL data into MongoDB using python script

Postby Ram668000 » Tue Nov 03, 2020 6:33 am

Hi Team,

Please help on bellow requirement.

Need to load data from SQL DB to Mongo DB using python script. Please help on same
Ram668000
Junior Member
Posts: 6
Joined: Fri Sep 25, 2020 7:29 am

Re: How to load SQL data into MongoDB using python script

Postby Ram668000 » Fri Nov 20, 2020 10:22 am

Hi Team,
Any update?
akshaysharma12
Junior Member
Posts: 6
Joined: Tue Jul 12, 2022 6:29 am

Re: How to load SQL data into MongoDB using python script

Postby akshaysharma12 » Fri Nov 18, 2022 7:40 am

What is SQL?

SQL(Structured Query Language) is a database programming language created for managing and retrieving data from relational databases. SQL is used to update, insert, delete, create and alter database tables and views, among other actions on the records kept in the database.

What is MongoDB?

The most popular NoSQL database and open-source document database is MongoDB. It is written in C++.

SQL:

1. By increasing the server's memory, storage space, or processing power, SQL databases can only be scaled vertically. In the case of large databases with a high query volume, vertical scaling can be expensive, with expenses rising quickly.

2. Many SQL databases were first created for the standalone servers. Their architecture changed to the distributed database, which increases robustness by running on a node's cluster, to reduce the failure risk. The database would still be available on other nodes in the cluster even if one node in the cluster went down.

3. The data in SQL databases must follow a predefined schema. For instance, the number of columns and data types must be specified when creating a table. Any data saved in the table must match the table's structure; otherwise, an error will be returned.

MongoDB:

1. Sharding, commonly known as horizontal scaling, is supported by MongoDB. In this instance, scalability is achieved by adding a new server rather than changing the server configuration. Because a cluster of inexpensive commodity hardware can efficiently meet the needs to support high query volume, this approach is typically less expensive.

2. Resilience was originally a consideration when NoSQL databases like MongoDB were created. For high reliability and availability, it runs on a cluster of inexpensive hardware and replicates the data among the nodes. In contrast to SQL databases, MongoDB architecture strongly emphasizes reliability and availability. As a result, MongoDB's automatic recovery is quicker and simpler than SQL databases.

3. There is no need to predefine any schema when using MongoDB. Different types of documents can be stored in collections without issues. If a new form of the document is introduced, there is no need to be concerned because it can be simply saved.

for detailed overview about mongodb introduction you can refer this: https://www.codingninjas.com/codestudio ... odb-part-1
bbyter
Junior Member
Posts: 1
Joined: Sat Jan 07, 2023 10:48 am

Re: How to load SQL data into MongoDB using python script

Postby bbyter » Sun Jan 08, 2023 11:13 am

Hi,

Once you have the source data in JSON format, the next step is to insert the data into a MongoDB collection. A collection is a set of documents and is the NoSQL equivalent of a table (or relation) in an RDBMS. We do that by calling the insert_many() method of the collection class, which returns the list of object ids of the inserted documents. Note that this method will throw an exception when an empty list is passed on as the argument, and hence the length check before the method call.

Python
import pymongo

mongodb_host = "mongodb://localhost:27017/"
mongodb_dbname = "mymongodb"

myclient = pymongo.MongoClient(mongodb_host)
mydb = myclient[mongodb_dbname]
mycol = mydb["categories"]

if len(myresult) > 0:
x = mycol.insert_many(myresult) #myresult comes from mysql cursor
print(len(x.inserted_ids))

After this step, you can check your MongoDB instance to verify that the database and collection have been created and the documents inserted. Note that MongoDB is schema-less, which means that you don't have to define the schema to insert documents, the schema is inferred on the fly and created automatically. MongoDB also creates the database and collection referenced in the code, if they do not already exist.
also working as a furniture.

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 25 guests