0

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.

1 Answer 1

6

I reformatted and patched the syntax error:

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 } } 
])

The problem was that you didn't have the $divide expression inside an object, so it was like

[ "$divide" : stuff, more, stuff]

instead of

[{ "$divide" : stuff }, more, stuff]

I can't help you with getting the aggregation to return what you want as I don't know exactly what you're looking for. If you need more help with that, could you edit the question to include a sample document and a description of what result you want to get from it?

Sign up to request clarification or add additional context in comments.

2 Comments

i edited my question adding some details of my actual mistake . I'm having problem with some documents that have null value on a date field and i cant aggregate them . (i am trying to exclude and count only the ones that exist in an especific field)
I edited by mistake your answer, can you delete my edits so others can see your answer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.