UE20MC505B Unit3 QuestionAnswers
UE20MC505B Unit3 QuestionAnswers
Question Answers
client = MongoClient('mongodb://localhost:27017/')
with client:
db = client.testdb
a = [{'$match': {'$or': [{'name': "Skoda"}, {'name': "Tata"}]}},
{'$group': {'_id' : 1, 'sum' : {'$sum':'$price'}}}]
v = list(db.cars.aggregate(a))
for i in range(len(v)):
print(v[i])
client = MongoClient('mongodb://localhost:27017/')
with client:
db = client.testdb
n_cars = db.cars.find().count()
client = MongoClient('mongodb://localhost:27017/')
with client:
db = client.testdb
exp_cars = db.cars.find({'price': {'$gt': 43000}})
for e in exp_cars:
print(e['name'])
client = MongoClient('mongodb://localhost:27017/')
with client:
db = client.testdb
for c in cars:
print(c)
NoSQL UE20MC505B
Question Answers
5. Aggregation in Python
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["pes"]
mycol = mydb["employee"]
def insert():
empId = input('Enter Employee id :')
empName = input('Enter Name :')
empSalary = int(input('Enter Salary :'))
empDept = input('Enter the Department')
empAge = int(input('Enter the Age'))
empCity = input('Enter the City')
mycol.insert_one(
{
"id": empId,
"name": empName,
"salary": empSalary,
"dept": empDept,
"age": empAge,
"city": empCity
})
n=input("Entering the number of documents needed")
for i in range(0,int(n)):
insert()