Search code examples
rmongodb

rmongodb: substract date in aggregation


After finally figuring out how to parameterize an aggregation pipeline with a date using mongo.bson.from.list(), I've been wrangling with the snippet below to get the $min for the date difference below, but keep getting

 Error in mongo.aggregation(m, collection , aggregation_pipeline ) : 
   mongoDB error: 10. Please check ?mongo.get.err for more details.

which means that my BSON group expression, although acceptable to mongo.bson.from.list(), is rejected by mongo.aggregation().

I've tried using list() rather than c(), and cod rather than its ISODate definition, with various quoting variations.

 cod <- as.POSIXct("2015-01-01 00:00:00" , tz = "GMT" )

 grouparg_bson1 <- mongo.bson.from.list(
   list( '$group' = list( '_id'     = list('ID'        = '$dcmt.cid') ,
                          'dtdiff'  = list('$min'      = list('$subtract' = c('$dcmt.orddt','ISODate("2015-01-01T00:00:00Z")' )) ),
                          'tota'    = list('$sum'      = '$dcmt.A' ),
                          'list'    = list('$addToSet' = '$dcmt.B' )
   ))
 )

I am using mongo's latest stable release, version 3.2.4.


Solution

  • Trial & error: the following works.

     grouparg_bson1 <- mongo.bson.from.list(
       list( '$group' = list( '_id'     = list('ID'        = '$dcmt.cid') ,
                              'dtdiff'  = list('$min'      = list('$subtract' = list('$dcmt.orddt', cod )) ),
                              'tota'    = list('$sum'      = '$dcmt.A' ),
                              'list'    = list('$addToSet' = '$dcmt.B' )
       ))
     )