Assignments Of Day 4: MongoDB Data Types
Assignments with Solutions
Assignment 1: Create a User Profile Document
Task:
Create a MongoDB document for a user's profile, which includes:
- A string for the name of the user.
- An integer for the age of the user.
- A boolean for account status (active or inactive).
- An array of strings for hobbies (e.g., "reading", "travelling").
- A date for the account creation date.
- An embedded document for the user's address with fields for street, city, and postal code.
Solution:
db.users.insertOne({
"name": "Alice",
"age": 30,
"isActive": true,
"hobbies": ["reading", "travelling"],
"createdAt": new Date("2023-05-20T10:00:00Z"),
"address": {
"street": "123 Maple St",
"city": "Springfield",
"postalCode": "12345"
}
});
Assignment 2: Add a Field for isPremium and lastLogin Date
Task:
Add a new boolean field isPremium to the user profile document to indicate if the user is a premium member. Add a lastLogin field with the date of the last login.
Solution:
db.users.updateOne(
{ "name": "Alice" },
{
$set: {
"isPremium": true,
"lastLogin": new Date("2023-08-15T14:30:00Z")
}
}
);
Assignment 3: Create a Product Document with Price and Stock
Task:
Create a MongoDB document for a product with:
- A string for the product name.
- A float for the price.
- An integer for the stock quantity.
- A boolean for availability.
- A date for the product release date.
Solution:
db.products.insertOne({
"productName": "Laptop",
"price": 999.99,
"stock": 50,
"isAvailable": true,
"releaseDate": new Date("2023-01-01T00:00:00Z")
});
Assignment 4: Query Users with Specific Hobbies
Task:
Write a query to find users who have "reading" as one of their hobbies.
Solution:
db.users.find({
"hobbies": "reading"
});
Assignment 5: Update Product Stock Using $inc Operator
Task:
Increase the stock of a product by 10 using the $inc operator. Assume the product name is "Laptop."
Solution:
db.products.updateOne(
{ "productName": "Laptop" },
{ $inc: { "stock": 10 } }
);
Assignment 6: Add a Comment Field to Product Document
Task:
Add a comments field to the "Laptop" product document, which stores an array of strings for customer reviews.
Solution:
db.products.updateOne(
{ "productName": "Laptop" },
{ $set: { "comments": ["Great product!", "Value for money."] } }
);
Assignment 7: Query Users Who Are Not Active
Task:
Write a query to find users who are not active (i.e., the isActive field is false).
Solution:
db.users.find({
"isActive": false
});
Assignment 8: Query Users With Date of Account Creation After a Specific Date
Task:
Write a query to find users who created their accounts after January 1st, 2023.
Solution:
db.users.find({
"createdAt": { $gt: new Date("2023-01-01T00:00:00Z") }
});
Assignment 9: Create an Embedded Document for an Order
Task:
Create a MongoDB document for an order with:
- A string for the order ID.
- A date for the order date.
- An array of products (with product name and quantity).
- A boolean to check whether the order is shipped or not.
- An embedded document for the shipping address with street, city, and postal code.
Solution:
db.orders.insertOne({
"orderId": "ORD12345",
"orderDate": new Date("2023-07-20T14:00:00Z"),
"products": [
{ "productName": "Laptop", "quantity": 1 },
{ "productName": "Mouse", "quantity": 2 }
],
"isShipped": false,
"shippingAddress": {
"street": "456 Oak St",
"city": "Greenfield",
"postalCode": "67890"
}
});
Assignment 10: Store a User's Profile Picture as Binary Data
Task:
Store a user's profile picture as binary data. Use the BinData type to store the image (you can use a placeholder string for simplicity).
Solution:
db.users.updateOne(
{ "name": "Alice" },
{ $set: { "profilePicture": BinData(0, "iVBORw0KGgoAAAANSUhEUgAA...") } }
);
