0% found this document useful (0 votes)
40 views4 pages

Awd Mongodb Command or Query

Uploaded by

beyou5360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Awd Mongodb Command or Query

Uploaded by

beyou5360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SDJ INTERNATIONAL COLLEGE, PALSANA.

ADVANCED WEB DESIGNING

MONGODB COMMANDS OR QUERY

Write command/query for following MongoDB operations :


Q.1
1. Create Database "University"
use University

2. Create Collection named "private"


db.createCollection("private")

3. Create Collection named "central_uni"


db.createCollection("central_uni")

4. Insert at least 2 documents in the collection "private" and in collection


"central_uni"
PRIVATE:
db.private.insertMany([
{ name: "C.J. Shah", location: "City A", category: "legit",
c_id: "201" },
{ name: "A.B. Patel", location: "City B", category: "fake",
c_id: "202" }
])

CENTRAL_UNI
db.central_uni.insertMany([
{ name: "J.R. Roy", location: "City C", contact_no:
"1234567890", c_id: "101" },
{ name: "M.S. Sharma", location: "City D", contact_no:
"9876543210", c_id: "102" }
])
5. Delete the private university having name "C.J. Shah"
db.private.deleteOne({ name: "C.J. Shah" })

6. Find central_uni university named "J.R.Roy"


db.central_uni.find({ name: "J.R. Roy" })

7. Update the "contact no" of central uni university where c_id is "101"
db.central_uni.updateOne(
{ c_id: "101" },
{ $set: { contact_no: "1112223334" } }
)

8. Delete all private university from "private" collection who have


category under the "fake" list.
db.private.deleteMany({ category: "fake" })

Q.2
1. Create Database "movie"
use movie

2. Create Collection named "film"


db.createCollection("film")

3. Create Collection named "actor"


db.createCollection("actor")

4. Insert at least 2 documents in the collection film and in collection actor


FILM:
db.film.insertMany([
{ title: "XYZ", genre: "Action", release_year: 2023 },
{ title: "ABC", genre: "Drama", release_year: 2022 }
])
ACTOR:
db.actor.insertMany([
{ name: "ABC", age: 45, actor_id: "101", address: "123 Street"
},
{ name: "DEF", age: 55, actor_id: "102", address: "456
Avenue" }
])

5. Delete the film "XYZ"


db.film.deleteOne({ title: "XYZ" })

6. Delete an actor named "ABC"


db.actor.deleteOne({ name: "ABC" })

7. Update the actor's address where actor id is "101"


db.actor.updateOne(
{ actor_id: "101" },
{ $set: { address: "789 New Street" } }
)
8. Delete all actors from an "actor" collection who have age greater than
"50"
db.actor.deleteMany({ age: { $gt: 50 } })

Q.3

1. Write a query to create "student" collection in the current database.

db.createCollection("student")

2. Write a query to insert five students' details into "student"


collection.

db.student.insertMany([

{ name: "John Doe", roll_no: 1, marks: 85, age: 20 },

{ name: "Jane Smith", roll_no: 2, marks: 90, age: 21 },

{ name: "Alice Johnson", roll_no: 3, marks: 75, age: 19 },


{ name: "Bob Brown", roll_no: 4, marks: 65, age: 22 },

{ name: "Charlie Davis", roll_no: 5, marks: 95, age: 20 }

])

3. Write a query to display students' names from "student" collection.

db.student.find({}, { name: 1, _id: 0 })

4. Write a query to display top 3 students' Roll no from "student"


collection.

db.student.find({}, { roll_no: 1, _id: 0 }).limit(3)

5. Write a query to display students' details with highest and lowest


marks from "student" collection.

Student with highest marks:

db.student.find().sort({ marks: -1 }).limit(1)

Student with lowest marks:

db.student.find().sort({ marks: 1 }).limit(1)

You might also like