
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
Contents
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 MongoClientThese 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.afzalThis 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 MongoClientThese 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.afzalThis 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.
6 thoughts on “Document Insertion to MongoDB”
어제 친구들과 회식 자리로강남가라오케추천다녀왔는데, 분위기도 좋고 시설도 깨끗해서 추천할 만했어요.
요즘 회식 장소 찾는 분들 많던데, 저는 지난주에강남가라오케추천코스로 엘리트 가라오케 다녀와봤습니다.
분위기 있는 술자리 찾을 땐 역시강남하퍼추천확인하고 예약하면 실패가 없더라고요.
회사 동료들이랑강남엘리트가라오케방문했는데, VIP룸 덕분에 프라이빗하게 즐길 수 있었어요.
신논현역 근처에서 찾다가강남룸살롱를 예약했는데, 접근성이 좋아서 만족했습니다.
술자리도 좋지만 요즘은강남셔츠룸가라오케이라고 불릴 만큼 서비스가 좋은 곳이 많더군요.