Docs Home → Develop Applications → MongoDB Manual
Aggregation Pipeline Operators
Aggregation Pipeline Stages
Stages
db.collection.aggregate( [ {
{ <field1>: <expression1>, ... }
Operator Expressions
{ <operator>: [ <argument1>, <argument2> … ] }
If operator accepts a single argument, you can omit the outer array designating the argument list:
{ <operator>: <argument> }
#dateAdd
# Embedded Document Fields
$lookup – Perform Multiple Joins and a Correlated Subquery with $lookup
$expr – Compare Two Fields from A Single Document
Practical MongoDB Aggregations Book
Creating Functions in MongoDB Console [duplicate] :
Call function inside mongodb’s aggregate?
Writing complex query in mongo DB :
Please Try the below query : Step 1 : we are filtering only those documents with "class" : "a" OR "class" : "b" and "hrs" key exists. Step 2 : we are doing multiplication of "rate" , "hrs" with 52 and storing the result in the key "rateMultiply" Step 3 : we are filtering the documents on the below mentioned criteria : class = "a" && rate > "20000" OR class = "b" && (rate X hrs X 52) > "20000" Step 4 : we are displaying the documnets with only those fields which we need. Lets create a collection " exp4 " and insert the below documents : db.exp4.insert({ "_id" : "3434sfsf", "rate" : 60, "class" : "a" }); db.exp4.insert({ "_id" : "sdsdsd", "rate" : 60, "class" : "b", "hrs" : 8 }); db.exp4.insert({ "_id" : "123", "rate" : 30000, "class" : "a", "hrs" : 8 }); db.exp4.insert({ "_id" : "12567", "rate" : 12000, "class" : "b" }); Now lets QUERY the collection : db.exp4.aggregate([ { $match: {$or : [ {"class" : "a"}, {$and : [{"class":"b"},{"hrs": {"$exists" : 1}}]} ] } }, { $project : { rateMultiply : { $multiply: ["$rate","$hrs",52]}, rate:1, class:1, hrs : 1 } }, { $match : {$or : [ { $and : [ {"class" : "a"} , {"rate" : {"$gt" : 20000}} ] } , { $and : [ {"class" : "b"}, {rateMultiply: {$gt:20000}} ] } ] } }, { $project: {class : 1 , rate : 1 , hrs : 1 } } ]) RESULT : { "_id" : "sdsdsd", "rate" : 60, "class" : "b", "hrs" : 8 } { "_id" : "123", "rate" : 30000, "class" : "a", "hrs" : 8 }
Call stored function in mongodb :
db.system.js.save({ _id: "echoFunction", value: function (x) { return 'echo: ' + x; } }) db.eval("echoFunction('test')") // -> "echo: test"