Python Data Access Tutorial
Selected Reading
- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python MongoDB - Insert Document
Python MongoDB - Insert Document
You can store documents into MongoDB using the insert() method. This method accepts a JSON document as a parameter.
Syntax
Following is the syntax of the insert method.
>db.COLLECTION_NAME.insert(DOCUMENT_NAME)
Example
> use mydb switched to db mydb > db.createCollection("sample") { "ok" : 1 } > doc1 = {"name": "Ram", "age": "26", "city": "Hyderabad"} { "name" : "Ram", "age" : "26", "city" : "Hyderabad" } > db.sample.insert(doc1) WriteResult({ "nInserted" : 1 }) >
Similarly, you can also insert multiple documents using the insert() method.
> use testDB switched to db testDB > db.createCollection("sample") { "ok" : 1 } > data = [ { "_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad" }, { "_id": "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" }, { "_id": "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" } ] [ { "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" }, { "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" }, { "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" } ] > db.sample.insert(data) BulkWriteResult ({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) >
Creating a collection using python
Pymongo provides a method named insert_one() to insert a document in MangoDB. To this method, we need to pass the document in dictionary format.
Example
Following example inserts a document in the collection named example.
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ mydb ] #Creating a collection coll = db[ example ] #Inserting document into a collection doc1 = {"name": "Ram", "age": "26", "city": "Hyderabad"} coll.insert_one(doc1) print(coll.find_one())
Output
{ _id : ObjectId( 5d63ad6ce043e2a93885858b ), name : Ram , age : 26 , city : Hyderabad }
To insert multiple documents into MongoDB using pymongo, you need to invoke the insert_many() method.
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ mydb ] #Creating a collection coll = db[ example ] #Inserting document into a collection data = [ { "_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad" }, { "_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore" }, { "_id": "103", "name": "Robert", "age": "28", "city": "Mumbai" } ] res = coll.insert_many(data) print("Data inserted ......") print(res.inserted_ids)
Output
Data inserted ...... [ 101 , 102 , 103 ]Advertisements