I'm having a hard time to get this code working . I have an aggregation query in mongo db in which I do some operations inside . The query without the $ifNull operator is working fine , however I'm getting some null values that I do not want to be printing. I've read the documentation of $ifNull operators , but I am not able to get this code working. I have this code so far , witch is giving me this error :
SyntaxError: Unexpected token :
I think that's the ":" from the ifnull but I don't know why. I'm following the sintax that is on the documentation in the official website. I've also tried to use another project field in order to filter the Null values , but it didn't work. This is de query so far :
db.cleandrivers.aggregate(
[
{
$project :
{ _id:"$_id",
week : {$week : "$recruiter.date"},
dif: {
$ifNull: [$divide : [{$subtract : ["$personal.stat.first_inspected","$recruiter.date"]} , 1000 * 60 * 60 ], "0"]
}
}
}, { $match : { week: 11 } }
])
Edit : Everything is working fine , this is a query that is actually working counting the recruited ppl giving an specific week.
db.cleandrivers.aggregate(
... [
... {
... $project :
... { _id:"$_id",
... week : {$week : "$personal.stat.recruited"}
... }
... },
... { $match : { $and: [ {week: 10}] } },
... {$group: { _id: "Sum",sum: {$sum : 1}}} ])
Output :
{ "_id" : "Sum", "sum" : 199 }
This is my document sintax :
"personal" : {
"stat" : {
"recruited" : ISODate("2015-02-02T14:30:32.089Z"),
"first_inspected" : ISODate("2015-02-02T14:33:15.956Z"),
"targo" : ISODate("2015-02-06T17:51:00.672Z")
}
The problem is that when I use the same query that i used when i was trying to count the recruited ppl , its giving me an error ,because there are some documents that do not have that field. I am really confused on how do I have to filter all the documents that have the field first_inspected in their documents , and count them giving a week . The error happens on the $week line witch tries to convert a null date into a week and it gives me an error.