mongodb - $add with some fields as Null returning sum value as Null -


these documents in collection

{ "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extramarks":10} { "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 } { "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 } 

only document 1 has field marks:

now have make projection sum of "final"+"midterm"+"extramarks"

i have written query projection follows:

db.students.aggregate([    {      $project: {          examtotal: { $add: [ "$final", "$midterm","$extramarks" ] }      }    } ]) 

this query giving me correct result document1 , doc2 , doc3, field doesnt exist giving sum null.

is possible check if field not null , add in query itself..any suggestions this?

is there functionality similar nvl (in sql) in queries mongo db?

you can 2 projections, first using $ifnull similar nvl:

db.students.aggregate([   { $project: { final: 1, midterm: 1, extramarks: { $ifnull: [ "$extramarks", 0 ] } } },    { $project: { examtotal: { $add: [ "$final", "$midterm","$extramarks" ] } } }  ]) 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -