MongoDB Questions and Queries
1. Retrieve the manufacturer, model, and price of all smartphones that cost less than 10000:
db.product_catalog.find(
{ price: { $lt: 10000 } },
{ manufacturer: 1, model: 1, price: 1, _id: 0 }
);
2. Retrieve the manufacturer and model of all phones running Android Oreo:
db.product_catalog.find(
{ android: 'Oreo' },
{ manufacturer: 1, model: 1, _id: 0 }
);
3. Retrieve the model and price of mobiles that have a battery above 3000:
db.product_catalog.find(
{ battery: { $gt: 3000 } },
{ model: 1, price: 1, _id: 0 }
);
4. Update the smartphones with additional attributes:
- For Mi Max 2:
db.product_catalog.updateOne(
{ manufacturer: 'mi', model: 'Max 2' },
{
MongoDB Questions and Queries
$set: {
memory: ['32 GB', '64 GB', '128 GB'],
price: 15000
);
- For Google Pixel 2:
db.product_catalog.updateOne(
{ manufacturer: 'google', model: 'Pixel 2' },
$set: {
colors: ['Clearly White', 'Jet Black', 'Very Silver'],
memory: ['32 GB', '64 GB', '128 GB']
);
5. Create a new collection called SortedByModel with the first 5 mobiles sorted by model name:
db.product_catalog.aggregate([
{ $sort: { model: 1 } },
{ $limit: 5 },
{ $out: "SortedByModel" }
]);
MongoDB Questions and Queries
6. Create a new collection called Under10k with all smartphones that cost less than 10000:
db.product_catalog.aggregate([
{ $match: { price: { $lt: 10000 } } },
{ $out: "Under10k" }
]);
7. Create a new collection called fifteenKAndAbove with all smartphones that cost 15000 or more:
db.product_catalog.aggregate([
{ $match: { price: { $gte: 15000 } } },
{ $out: "fifteenKAndAbove" }
]);
8. Create an ascending single field index on the price field:
db.product_catalog.createIndex({ price: 1 }, { name: "asc_price" });
9. Create a descending single field index on the model field:
db.product_catalog.createIndex({ model: -1 }, { name: "desc_model" });