Document Insertion to MongoDB

Document Insertion to MongoDB

In MongoDB, efficient document insertion is key to maintaining a well-organized and responsive database. Whether inserting a single document or multiple documents at once, MongoDB offers straightforward methods for seamlessly integrating data into your collections.

Inserting a Single Document into The MongoDB

In MongoDB, inserting a single document involves creating a dictionary representing the data to be inserted and using the insert_one() method to insert it into the desired collection. For example, if we want to insert a single student document with attributes such as ID and name into a collection named “students”, we define the document as a dictionary, specify the collection, and call insert_one() to insert it.

import pymongo
from pymongo import MongoClient

# Establish a connection to the MongoDB server
client = pymongo.MongoClient("mongodb+srv://badshah:password@cluster.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")

# Select the database and collection
db = client.afzal
collection = db.afzal

# Define a dictionary representing a student document
std = {"id": "123", "name": "afzal"}

# Insert the document into the collection
collection.insert_one(std)

Explanation

Import MongoDB modules

import pymongo
from pymongo import MongoClient

These lines import the necessary modules from pymongo library for interacting with MongoDB.

Establish a connection to the MongoDB server

client = pymongo.MongoClient("mongodb+srv://badshah:password@cluster.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")

This line establishes a connection to the MongoDB server hosted at the specified URI. Replace the URI with your own MongoDB server URI.

Select database and collection

db = client.afzal
collection = db.afzal

This code selects the database named “afzal” and the collection named “afzal” within that database. Replace “afzal” with your preferred database and collection names.

Define student document

std = {"id": "123", "name": "afzal"}

This line defines a dictionary representing a student document to be inserted into the collection. It contains key-value pairs for student ID and name.

Insert a document into the collection

collection.insert_one(std)

This line inserts the student document into the selected collection. The insert_one() method inserts a single document into the collection.

Inserting Multiple Documents into The MongoDB

In MongoDB, inserting multiple documents is done by creating a list of dictionaries, each representing a document, and using the insert_many() method to insert all the documents at once into the collection. This method is efficient for bulk data insertion. For instance, if we have a list of student documents to be inserted into the “students” collection, we create the list of dictionaries, specify the collection, and call insert_many() to insert all documents simultaneously.

import pymongo
from pymongo import MongoClient

# Establish a connection to the MongoDB server
client = pymongo.MongoClient("mongodb+srv://badshah:password@cluster.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")

# Select the database and collection
db = client.afzal
collection = db.afzal

# Define a list of dictionaries representing student documents
std = [
  {"id": "123", "name": "afzal"},
  {"id": "124", "name": "afzal"},
  {"id": "125", "name": "afzal"},
  {"id": "126", "name": "afzal"}
]

# Insert multiple documents into the collection
collection.insert_many(std)

Explanation

Import MongoDB modules

import pymongo
from pymongo import MongoClient

These lines import the necessary modules from pymongo library for interacting with MongoDB.

Establish a connection to the MongoDB server

client = pymongo.MongoClient("mongodb+srv://badshah:password@cluster.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")

This line establishes a connection to the MongoDB server hosted at the specified URI. Replace the URI with your own MongoDB server URI.

Select database and collection

db = client.afzal
collection = db.afzal

This code selects the database named “afzal” and the collection named “afzal” within that database. Replace “afzal” with your preferred database and collection names.

Define a list of student documents:

std = [
  {"id": "123", "name": "afzal"},
  {"id": "124", "name": "afzal"},
  {"id": "125", "name": "afzal"},
  {"id": "126", "name": "afzal"}
]

This line defines a list of dictionaries, with each dictionary representing a student document to be inserted into the collection. Each dictionary contains key-value pairs for student ID and name.

Insert multiple documents into the collection:

collection.insert_many(std)

This line inserts the list of student documents into the selected collection using the insert_many() method, which inserts multiple documents into the collection at once.

You can access the detailed tutorial here.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this:
Verified by MonsterInsights