Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

MongoDB Introduction

MongoDB Setup

MongoDB DataBase

MongoDB Insert Data

MongoDB Read Document

MongoDB Update

MongoDB Delete

MongoDB Sorting Index

MongoDB Aggregation

MongoDB Mongodb with python

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Work with mongodb using python

How to connect python with mongodb and create a database?

client=pymongo.MongoClient("mongodb://127.0.0.1:27017//")

#create database
mydb=client["first_database"]

How to create collection and insert document in mongodb using python?

client=pymongo.MongoClient("mongodb://127.0.0.1:27017//")

#create database
mydb=client["first_database"]

#create collection
c1=mydb.first_collection
record_1={
Books:"Python",Price:500,Type:"Programming",Email:"aaaa@gamil.com"
}
c1.insertOne(record_1)

record_2=[
{Books:"Python",Price:500,Type:"Programming",Email:"aaaa@gamil.com"},
{Books:"Javascript",Price:400,Type:"Programming",Email:"ba@gamil.com"},
{Books:"DS math",Price:300,Type:"Math",Email:"caa@gamil.com"}
]
c1.insert_many(record_2)

How to Read document in mongodb using python?

#Get a first document
mydb.find_one()

#get all the document
mydb.find({})

How to get values using condition in mongodb using python?

Example 1: Get all the documents of that collection where we have python.
for i in mydb.find({Books:"Python"}):
  print(i)

Example 1: Get documents where Books value is python and Javascript Example:

Use of in

in works like in operator python.

#
for i in mydb.find({Books:{"$in":["Python","Javascript"]}}):
  print(i)

Example 1: Get values where Books is python and Price is less than 300
# $lt means less than.
for i in mydb.find({Books:"Python",Price:{"$lt":300}}):
  print(i)

Use of or

or works like or operator in python .

Example 1:Get documents where Books value is python or Type is programming
for i in mydb.find({"%or":[{Books:"Python"},{Type:"Programming"}]}):
  print(i)

Use of and

and works like and operator in python .

Example: Get documents where Books value is python and Price is 300
for i in mydb.find({"%and":[{Books:"Python"},{Price:300}]}):
  print(i)

How to update records or documents in mongodb using python?

#Update one document using update_one
mydb.update_one(
{Price:300},{"$set":{Books:"DT",Type:"Science"}} )

#Update many document using update_many
mydb.update_many(
{Price:300},{"$set":{Books:"DT",Type:"Science"}}, {Type:"Programming"},{"$set":{Books:"P-books",Type:"P"}} )

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.