https://github.com/vidhya0501/day35task35_be_mongodb_task_vidhya
MongoDB queries to display product details based on the requirement.
https://github.com/vidhya0501/day35task35_be_mongodb_task_vidhya
database json mongo mongodb
Last synced: 3 months ago
JSON representation
MongoDB queries to display product details based on the requirement.
- Host: GitHub
- URL: https://github.com/vidhya0501/day35task35_be_mongodb_task_vidhya
- Owner: Vidhya0501
- Created: 2024-02-28T13:31:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-28T13:55:54.000Z (over 2 years ago)
- Last Synced: 2025-03-01T10:30:51.980Z (over 1 year ago)
- Topics: database, json, mongo, mongodb
- Homepage:
- Size: 1.77 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
MongoDB
Create database and insert documents in collection.
use productDatabase
db.products.insertMany()
1. Find all the information about each products
db.products.find().pretty();
2. Find the product price which are between 400 to 800
db.products.find({product_price:{$gte:400,$lte:800}}).pretty();
3. Find the product price which are not between 400 to 600
db.products.find({product_price:{$not:{$gte:400,$lte:600}}}).pretty();
4. List the four product which are greater than 500 in price
db.products.find({product_price:{$gt:500}}).limit(4).pretty();
Only three products are available with price greater than 500.
If we give greater than or equal to 500 then we can get four products.
db.products.find({product_price:{$gte:500}}).limit(4).pretty();
5. Find the product name and product material of each products
db.products.find({},{product_name:1,product_material:1}).pretty();
6. Find the product with a row id of 10
db.products.findOne({id:"10"});
7. Find only the product name and product material
db.products.find({},{_id:0,product_name:1,product_material:1}).pretty();
8. Find all products which contain the value of soft in product material
db.products.find({product_material:{$eq:"Soft"}}).pretty();
9. Find products which contain product color indigo and product price 492.00
None. No products have product color “indigo” and product price “492.00”.
But if we can retrieve products which have either product color “indigo” or product price “492.00”.
db.products.find({$or:[{product_color:{$eq:"indigo"}},{product_price:{$eq:492.00}}]}).pretty();
10. Delete the products which product price value are same
var pricesToDelete=db.products.aggregate([{$group:{_id:"$product_price",count:{$sum:1},docs:{$push:"$_id"}}},{$match:{count:{$gt:1}}}]).toArray();
pricesToDelete.forEach(function(priceGroup){db.products.deleteMany({_id:{$in:priceGroup.docs}});});
Initially we have 25 products, in that product_prices 47 and 36 occur twice.
After deleting those now we have 21 products.