Re: Difference In Dates in Days - Cannot Subtract String From String Error

From: Wan Bachtiar <wan.bachtiar@xxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Wed, 11 May 2016 19:16:49 -0700 (PDT)
Why ads?


Should I be using $project or something similiar in this?

Hi, 

You could use $project 
<https://docs.mongodb.com/manual/reference/operator/aggregation/project/
to create a new field that contains the time difference, which you could 
pass into the next aggregation operator. See Aggregation Pipeline Operators 
<https://docs.mongodb.com/manual/reference/operator/aggregation/> for more 
operators.

Uncaught exception ‘MongoResultException’ with message ‘localhost:27017: 
cant $subtract aString from a String’

Based on your PHP exception class MongoResultException 
<http://php.net/manual/en/class.mongoresultexception.php>, it seems that 
you are using MongoDB Driver (legacy) 
<http://php.net/manual/en/book.mongo.php>. To match the driver version that 
you are using, the following example is written in MongoDB Driver (legacy) 
v1.6, PHP5 and MongoDB v3.0. 

The difference with the previous example is the use of MongoDate 
<http://php.net/manual/en/class.mongodate.php> class instead of 
MongoDB\BSON\UTCDateTime 
<http://php.net/manual/en/class.mongodb-bson-utcdatetime.php> class.

/* reminder time in milliseconds */$reminder_interval = 60 * 1000;/* Get current time in milliseconds */$current_time = new MongoDate();$query = array(
            array('$project'=>
              array('_id'=> 1,
                    'other_fields'=> 1,
                    'time_diff'=> array('$subtract'=>
                      array($current_time, '$last_interacted_date_field')
                    )
              )
            ),
            array('$redact' =>
              array('$cond' => 
                array("if" => 
                  array('$gte' => 
                    array('$time_diff',
                          $reminder_interval
                    )
                  ),
                  "then" => '$$KEEP',`
                  "else" => '$$PRUNE'
                )
             )
           )
        );

If you have further questions, could you provide the following:

   - MongoDB version. 
   - MongoDB PHP driver version. 

Kind regards, 

Wan. 





-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/d1c195ac-cec6-4de1-b5e3-c36075ecb602%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?