MongoDB :
Get started MongoDB and Following: MongoDB.com
Get started MongoDB Operator and Following: Operator
In this example , "practice" is a Database collection.
db.practice.find(
{ age: { $eq: 18 } }
)
db.practice.find(
{ age: { $gt: 22 } }
)
db.practice.find(
{ age: { $gte: 24 } }
)
db.practice.find(
{ age: { $in: [26,17] } }
)
db.practice.find(
{ age: { $lt: 26 } }
)
db.practice.find(
{ age: { $lte: 26 } }
)
db.practice.find(
{ age: { $ne: 26 } }
)
db.practice.find(
{ age: { $nin: [26,17] } ,
{ gender: {$gt : 24}}}
)
$and Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
db.practice.find(
{ $and: [
{age :{ $lt: 24 }},
{gender: "Female"}
] }
).project({
age: 1,
gender: 1
})
$not Inverts the effect of a query expression and returns documents that do not match the query expression.
db.practice.find(
{ age :{ $not:{ $gt:24} }},
{ gender: "Female" }
).project({
age: 1,
gender: 1
})
db.practice.find(
{$nor:[
{ age :{ $lte:23} },
{ gender: "Female" }
]}
).project({
age: 1,
gender: 1
})
$or Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
db.practice.find(
{ $or: [
{age :{ $gt: 17 }},
{gender: "Female"}
] }
).project({
age: 1,
gender: 1
})
db.practice.find(
{ skills :{ $exists : true} },
).project({
skills: 1,
})
db.practice.find(
{ skills :{ $type : 'int' } },
).project({
skills: 1,
})
Aggregation is a way of processing a large number of documents in a collection by means of passing them through different stages. The stages make up what is known as a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify documents that pass through the pipeline.
This is an example of the aggregation pipeline syntax:
pipeline = [
{ $match : { … } },
{ $group : { … } },
{ $sort : { … } }
]
Some Example for aggregation :
db.practice.aggregate(
{
$match : {
gender : 'Male' ,
age : {$gt :18}
}
}
)
If you want to use project then:
db.practice.aggregate(
{
$match : {
gender : 'Male' ,
age : {$gt :18}
}
},
{
$project: {
gender: 1 ,
age: 1
}
}
)