Query Collection
Query Collection
#3. Get all categories with id = 2001 //SELECT * FROM categories WHERE id = 2001
db.categories.find({_id: 2001})
#6. Get all products whose price is more than 30000 // WHERE price>30000
db.products.find({price:{$gt: 30000}})
#10. Get all products whose category id is not equal to 2001 //WHERE NOT
category_id = 2001
db.products.find({category_id: {$ne: 2001}})
#12. Get all products whose price is more that 45000 and less than 50000 //WHERE
price>45000 and price<50000
db.products.find({price: {$gt: 45000, $lt: 50000}})
db.products.find(
{
$and: [
{price: {$gt: 45000}},
{price: {$lt: 50000}}
]
})
#13. Get all products whose price is greater than 10000 and category id is 2003
db.products.find(
{
$and: [
{price: {$gt: 10000}},
{category_id: 2003}
]
})
#14. Get all products whose price is less than 10000 or greater than 50000
db.products.find(
{
$or:
[
{price: {$lt: 10000}}, {price: {$gt: 50000}}
]
})
#15. Get all products whose price is less than 10000 or greater than 50000 and
category_id is 2003 or company id is 1001
--1.
db.products.find(
{
$and:[
{$or: [
{price: {$lt: 10000}}, {price: {$gt: 50000}}
] }
}
)
-- 2.
db.products.find(
{$or:[
{category_id: 2001}, {company_id: 1001}
]}
)
db.products.find(
{
$and:[
{$or: [
{price: {$lt: 10000}}, {price: {$gt: 50000}}
] },
{$or:[
{category_id: 2001}, {company_id: 1001}
]}
]})
db.orders.find(
{
$expr:{
$eq:[{"$year":"$order_date"}, 2013]
}
})
db.customers.find(
{
mobileno: {$all: ['8963521478', '945936245']}
}
)
#27. Get all orders of the year 2013 which is having product_id as 9
db.orders.find(
{
"orderItems.product_id": 9,
"order_date" :{
$gte: ISODate('2013-01-01'),
$lte: ISODate('2013-12-31')
}
}, {"orderItems.$": 1, _id: 0})