Skip to content

Instantly share code, notes, and snippets.

@deokarniharika
Last active September 11, 2022 13:12
Show Gist options
  • Save deokarniharika/fd7e71808782ef4e469e6b76506875f5 to your computer and use it in GitHub Desktop.
Save deokarniharika/fd7e71808782ef4e469e6b76506875f5 to your computer and use it in GitHub Desktop.
MongoDB assignments
/* MongoDB queries from BASICS to INTERMEDIATE
- CRUD
- Aggregation
- Functions, Operators, etc. */
// Create/use/switch to the named database
use university
// Show all the collections that are available in the particular database
show collections
// Show all the databases available
show dbs
// Create collection in selected database
db.createCollection(“stud”)
// Find all the documents in the said collection
db.stud.find()
// Insert a document
db.stud.insertOne({id: 6,
name: 'Niharika',
branch: 'CSE',
age: 19,
address: { city: Pune', pincode: 411004 },
subjects:
[ { name: 'TOC', score: 99 },
{ name: 'CN', score: 87 },
{ name: 'DBMS', score: 97 } ],
interests: [ 'Coding', 'Writing', 'Music' ] })
// Find all the documents in this particular collection
db.stud.find()
// Use the updateOne function
db.stud.updateOne({name: "Anushka"},{$pop:{interests: -1}})
// Use the update function and setting the age
db.stud.update({name:"Niharika"},{$set: {age: 19}},{upsert: true})
db.stud.updateOne(
{ name: "Pallavi" },
{ $set: { age: 22 } })
// Use the find() method
db.stud.find({interests: {$all: ["Coding", "Writing"]}})
// Use the sort() method
db.stud.find().sort({"name": 1})
// Find the number of interests
db.stud.find({interests:{$size: 2}});
// Use OR operator
db.stud.find({$or: [{branch: "CSE"}, {age: 19}]})
// Find the total number of documents in the collection stud
db.stud.find().count()
// Remove one interest by using the pop() function
db.stud.updateOne(
{ name: "Achintya" },
{ $pop: { interests: 1 } })
// Use AND operator
db.stud.find({$and: [{branch: "CSE"}, {age: 19}]})
// Find the number of students whose ages is greater than 20 years of age.
db.stud.find({age: {$gte: 20}})
import pymongo
cl = pymongo.MongoClient("mongodb://localhost:27017/")
print(cl.list_database_names())
from pymongo import MongoClient
#Creating a pymongo client
client = MongoClient('localhost', 27017)
#switching to the database- niharika
db=client['niharika']
coll=db['stud']
a=input("Enter number")
print(a)
n=input("Enter the details of number of students to be added:")
nn=int(n)
print("Enter student details")
for i in range(0,nn):
id=input("Enter student id")
name=input("Name: ")
branch=input("Branch")
age=input("Age: ")
sub1Name=input("Enter subject 1 name")
sub1Score=input("Enter subject1 score")
sub2Name=input("Enter subject 2 name")
sub2Score=input("Enter subject2 score")
d={"id": id,"name": name, "branch": branch, "age": age,
"address": {"city": "Mumbai", "pincode": 410001},
"subjects": [{"name": "TOC", "score": 89},
{ "name": "CN","score": 99 },
{"name": "DBMS", "score": 99 }],
"interests": ["Reading","Coding","Music"]}
print(d)
coll=db['stud']
r=coll.insert_one(d)
data={"id": 4,"name": "Samira", "branch": "Civil", "age": 22,
"address": {"city": "Mumbai", "pincode": 410001},
"subjects": [{"name": "TOC", "score": 89},
{ "name": "CN","score": 99 },
{"name": "DBMS", "score": 99 }],
"interests": ["Reading","Coding","Music"]}
res=coll.insert_one(data)
[{"_id": {
"$oid": "62e903ed0b1d91670ff35bcf"},"id": 1,"name": "Achintya", "branch": "CSE", "age": 22,
"address": {"city": "Pune", "pincode": 412101},
"subjects": [{"name": "TOC", "score": 97},
{ "name": "CN","score": 100 },
{"name": "DBMS", "score": 99 }],
"interests": ["Reading","Football"]},
{"_id": {
"$oid": "62e904250b1d91670ff35bd0"},"id": 2,"name": "Niharika", "branch": "CSE", "age": 20,
"address": {"city": "Pune", "pincode": 411001},
"subjects": [{"name": "TOC", "score": 99},
{ "name": "CN","score": 100 },
{"name": "DBMS", "score": 91 }],
"interests": ["Reading","Art","Music"]},
{"_id": {
"$oid": "62e904570b1d91670ff35bd1"},"id": 3,"name": "Anupriya", "branch": "CSE", "age": 19,
"address": {"city": "Pune", "pincode": 412104},
"subjects": [{"name": "TOC", "score": 99},
{ "name": "CN","score": 100 },
{"name": "DBMS", "score": 100 }],
"interests": ["Reading","Basketball","Coding"]},
{"_id": {
"$oid": "62e9048b0b1d91670ff35bd2"},"id": 4,"name": "Tanish", "branch": "CSE", "age": 19,
"address": {"city": "Mumbai", "pincode": 410001},
"subjects": [{"name": "TOC", "score": 89},
{ "name": "CN","score": 90 },
{"name": "DBMS", "score": 99 }],
"interests": ["Reading","Coding","Music"]},
{"_id": {
"$oid": "62e904570b1d91670ff35bd3"},"id": 5,"name": "Shilpa", "branch": "ECE", "age": 22,
"address": {"city": "Thane", "pincode": 412102},
"subjects": [{"name": "PS", "score": 89},
{ "name": "EVS","score": 100 },
{"name": "CN", "score": 100 }],
"interests": ["Reading","Art","Singing"]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment