sample questions 3

 set1


  1. Find all documents in a collection named "books".
  2. Find all documents in a collection named "customers" where the city is equal to "New York".
  3. Find a document in a collection named "users" where the username is equal to "johndoe"
{
  "title": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "year": 1925,
  "pages": 180
},
{
  "title": "1984",
  "author": "George Orwell",
  "year": 1949,
  "pages": 328
},
{
  "title": "To Kill a Mockingbird",
  "author": "Harper Lee",
  "year": 1960,
  "pages": 281
}

customers

{ "name": "John Doe", "city": "New York", "age": 30, "email": "johndoe@example.com" }, { "name": "Jane Smith", "city": "Los Angeles", "age": 25, "email": "janesmith@example.com" }, { "name": "Bob Johnson", "city": "Chicago", "age": 40, "email": "bjohnson@example.com" }

users

{
  "username": "johndoe",
  "password": "password123",
  "email": "johndoe@example.com",
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "state": "NY",
    "zip": "10001"
  }
},
{
  "username": "janesmith",
  "password": "password456",
  "email": "janesmith@example.com",
  "address": {
    "street": "456 Elm St",
    "city": "Los Angeles",
    "state": "CA",
    "zip": "90001"
  }
},
{
  "username": "bjohnson",
  "password": "password789",
  "email": "bjohnson@example.com",
  "address": {
    "street": "789 Oak St",
    "city": "Chicago",
    "state": "IL",
    "zip": "60601"
  }
}

solution:

db.books.find();

O/p:

gives all books

db.customers.find({ city: "New York" });

op

[  {    "_id" : ObjectId("62031c236e11d2af9e1f0db4"),    "name" : "John Doe",    "city" : "New York",    "age" : 30,    "email" : "johndoe@example.com"  }]

db.users.findOne({ username: "johndoe" });

op:
{
  "_id" : ObjectId("62031c726e11d2af9e1f0db5"),
  "username" : "johndoe",
  "password" : "password123",
  "email" : "johndoe@example.com",
  "address" : {
    "street" : "123 Main St",
    "city" : "New York",
    "state" : "NY",
    "zip" : "10001"
  }
}

Set2

  1. Insert a new document into a collection named "products" with the following fields: name, category, price, and quantity.
  2. Update the price of a product in a collection named "products" where the product ID is equal to 1234.
  3. Delete a document from a collection named "orders" where the order ID is equal to 5678.
data

products

{ "name": "iPhone 12", "category": "Electronics", "price": 999, "quantity": 10 }, { "name": "MacBook Air", "category": "Electronics", "price": 1299, "quantity": 5 }, { "name": "Nike Air Max", "category": "Shoes", "price": 150, "quantity": 20 }

orders

{ "order_id": 1234, "product_id": "iphone12", "quantity": 2, "total_price": 1998 }, { "order_id": 5678, "product_id": "nikeairmax", "quantity": 1, "total_price": 150 }, { "order_id": 9012, "product_id": "macbookair", "quantity": 1, "total_price": 1299 }, { "order_id": 3456, "product_id": "iphone12", "quantity": 1, "total_price": 999
}

solution:

db.products.insertOne({ name: "iPhone 13", category: "Electronics", price: 999.99, quantity: 10 });

op

{ "acknowledged" : true, "insertedId" : ObjectId("620307b53a4387f1b1d2492f") }

db.products.updateOne( { _id: ObjectId(give object id) }, { $set: { price: 799.99 } } );

op:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

db.orders.deleteOne({ _id: ObjectId(give object id in quoes) });

o/p:
{ "acknowledged" : true, "deletedCount" : 1 }

set 3:

1. Create a new database called store and a collection called indian_customers. 2. Insert 3 new documents into the customers collection, each with the fields name, email, phone, and address. 3. Query the customers collection and retrieve all documents. 4. Query the customers collection and retrieve only the name and email fields of all documents. 5. Update the document for a specific customer by its email field to change its phone
// Create the store database
use store

// Create the indian_customers collection and insert 3 new documents
db.indian_customers.insertMany([
  {
    name: "Rajesh Sharma",
    email: "rajesh@example.com",
    phone: "555-1234",
    address: "123 Main St."
  },
  {
    name: "Neha Gupta",
    email: "neha@example.com",
    phone: "555-5678",
    address: "456 Oak Ave."
  },
  {
    name: "Sanjay Patel",
    email: "sanjay@example.com",
    phone: "555-9012",
    address: "789 Elm St."
  }
])

// Query the indian_customers collection and retrieve all documents
db.indian_customers.find()

// Query the indian_customers collection and retrieve only the name and email fields of all documents
db.indian_customers.find({}, {name: 1, email: 1, _id: 0})

// Update the phone of a specific customer document by its email field
db.indian_customers.updateOne({email: "rajesh@example.com"}, {$set: {phone: "555-4321"}})

// Confirm the update
db.indian_customers.find({email: "rajesh@example.com"}) 

O/p: 
similar to set 2 output

set 4:

1. Create an index on the name field of a collection called students. create 3 documents in student collection with fields name, age, semester and degree 2. Query the students collection and retrieve all documents sorted by the name field in ascending order. 3. Query the students collection and retrieve only the documents where the name field contains the word "John". 4. Create a compound index on the name and age fields of the students collection. 5. Query the students collection and retrieve only the documents where the name field contains the word "John" and the age field is greater than or equal to 18

Solution:

db.students.createIndex({"name": 1})

also insert 3 documents as per q.1

db.students.find().sort({"name": 1})

o/p;
{ "_id" : ObjectId("..."), "name" : "John v v", "age" : 20, "semester" : 2, "degree" : "Computer Science" }
{ "_id" : ObjectId("..."), "name" : "Mary", "age" : 22, "semester" : 4, "degree" : "Business" }
{ "_id" : ObjectId("..."), "name" : "Tom", "age" : 19, "semester" : 3, "degree" : "Engineering" }


db.students.find({"name": /John/})

o/p:

{ "_id" : ObjectId("..."), "name" : "John", "age" : 20, "semester" : 2, "degree" : "Computer Science" }


db.students.createIndex({"name": 1, "age": 1})

db.students.find({"name": /John/, "age": {"$gte": 18}})

o/p:

{ "_id" : ObjectId("..."), "name" : "John", "age" : 20, "semester" : 2, "degree" : "Computer Science" }



set 5:

Banks documents to insert

{
  "_id": 1,
  "name": "Bank of America",
  "address": {
    "street": "123 Main St",
    "city": "Los Angeles",
    "state": "CA",
    "zip": "90001"
  },
  "total_assets": 1000000000,
  "num_employees": 5000
},
{
  "_id": 2,
  "name": "JP Morgan Chase",
  "address": {
    "street": "456 Elm St",
    "city": "New York",
    "state": "NY",
    "zip": "10001"
  },
  "total_assets": 2000000000,
  "num_employees": 10000
},
{
  "_id": 3,
  "name": "Wells Fargo",
  "address": {
    "street": "789 Oak St",
    "city": "San Francisco",
    "state": "CA",
    "zip": "94101"
  },
  "total_assets": 1500000000,
  "num_employees": 7500
}

  1. Write a query to find all banks that are located in California.
  2. Write a query to find the bank with the highest number of employees.
  3. Write a query to update the "total_assets" field of the bank with the _id of 1 to 1200000000.
db.banks.find({"address.state": "CA"})
db.banks.find().sort({"num_employees": -1}).limit(1)
db.banks.update({"_id": 1}, {"$set": {"total_assets": 1200000000}})

6. 

Projects collection

{
  "_id": 1,
  "name": "Project 1",
  "status": "in_progress",
 "budjet":150,000,
  "tasks": [
    {
      "name": "Task 1",
      "status": "completed"
    },
    {
      "name": "Task 2",
      "status": "in_progress"
    },
    {
      "name": "Task 3",
      "status": "not_started"
    }
  ]
},
{
  "_id": 2,
  "name": "Project 2",
  "status": "not_started",
 "budjet":170,000,
  "tasks": [
    {
      "name": "Task 1",
      "status": "not_started"
    },
    {
      "name": "Task 2",
      "status": "not_started"
    },
    {
      "name": "Task 3",
      "status": "not_started"
    }
  ]
},
{
  "_id": 3,
  "name": "Project 3",
  "status": "completed",
    "budjet":50,000,
  "tasks": [
    {
      "name": "Task 1",
      "status": "completed"
    },
    {
      "name": "Task 2",
      "status": "completed"
    },
    {
      "name": "Task 3",
      "status": "completed"
    }
  ]
}

  1. Write a query to find all projects that are not started.
  2. Write a MongoDB query to find all the projects that have a status of "completed" and a budget greater than $100,000.
  3. Write a MongoDB query to find the project with the highest value in the "budjet" field.

db.projects.find({"status": "not_started"})
db.projects.find({ status: "completed", budget: { $gt: 100000 } })

db.projects.find().sort({ budjet: -1 }).limit(1)

7.

 

Write a Python program in Jupyter to create following documents in MongoDB

 

DB: Fruits

Collection: New

Documents:

                { name: banana, color:yellow, no:5, price:10}

                { name: grape, color:dark blue, no:100, price:2}

 

DB: Countries

Collection: New1

Documents:

                { name: India, capital:delhi, no_states:28 }

                { name: USA, capital:Washington DC, no_states:50}

 

 

Solution:

 

pip install pymongo

 

import pymongo

 

# Connect to MongoDB server

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

 

# Create the Fruits database and New collection

db = client["Fruits"]

collection = db["New"]

 

# Define the documents to insert

document1 = {

    "name": "banana",

    "color": "yellow",

    "no": 5,

    "price": 10

}

 

document2 = {

    "name": "grape",

    "color": "dark blue",

    "no": 100,

    "price": 2

}

 

# Insert the documents into the collection

collection.insert_many([document1, document2])

 

 

 

# Create the Countries database and New1 collection

db = client["Countries"]

collection = db["New1"]

 

# Define the documents to insert

document1 = {

    "name": "India",

    "capital": "delhi",

    "no_states": 28

}

 

document2 = {

    "name": "USA",

    "capital": "Washington DC",

    "no_states": 50

}

 

# Insert the documents into the collection

collection.insert_many([document1, document2])

 

8. Write a Python program in Jupyter to create following documents in MongoDB

 

DB: Sports

Collection: New1

Documents:

                { name: cricket, level:international, no:11, players:{batsmen:[ram,lokesh,veeru,bodhan,rajesh],bowlers:[venkat,kapil,gowtham],Allrounders:[jaiswal,raju] WK:[kethan]}}

 

                { name: hockey, level:national, no:10, players:{GK:1,Offense:4,defense:5}

 

 

Solution:

 

pip install pymongo

import pymongo

 

# Connect to MongoDB server

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

 

# Create the Sports database and New1 collection

db = client["Sports"]

collection = db["New1"]

 

# Define the documents to insert

document1 = {

    "name": "cricket",

    "level": "international",

    "no": 11,

    "players": {

        "batsmen": ["ram", "lokesh", "veeru", "bodhan", "rajesh"],

        "bowlers": ["venkat", "kapil", "gowtham"],

        "Allrounders": ["jaiswal", "raju"],

        "WK": ["kethan"]

    }

}

 

document2 = {

    "name": "hockey",

    "level": "national",

    "no": 10,

    "players": {

        "GK": 1,

        "Offense": 4,

        "defense": 5

    }

}

 

# Insert the documents into the collection

collection.insert_many([document1, document2])

 

Comments

Popular posts from this blog

AIDS Meanstack Sample questions

Express.js Registration form data 2 Server