javascript - MongoDB : $hour operator does not accept an object as an operand -


i'm experiencing trouble trying hour collection , subtracting 3 hours (gmt-3)

if run

   db.post.aggregate(    [{$match : {'uri' : /fantastic/ }   },      {        $project:          {            _id: 0,            created_at : 1,            date_minus_3 : {$subtract:["$created_at",3*60*60*1000]},            hour_from_original: { $hour: "$created_at" }          }      }    ] ) 

i get

(...)     {         "created_at" : isodate("2014-06-03t02:51:42.000z"),         "date_minus_3" : isodate("2014-06-02t23:51:42.000z"),         "hour_from_original" : 2     },  (...) 

but if hour "date_minus_3"

   db.post.aggregate(    [{$match : {'uri' : /fantastic/ }   },      {        $project:          {            _id: 0,            created_at : 1,            date_minus_3 : {$subtract:["$created_at",3*60*60*1000]},            hour_from_original: { $hour: "$created_at" },            hour_from_date_minus_3 : {$hour: {$subtract:["$created_at",3*60*60*1000]}}          }      }    ] ) 

i error message

    error("printing stack trace")@:0 ()@src/mongo/shell/utils.js:37 ([object array])@src/mongo/shell/collection.js:866 @(shell):10  uncaught exception: aggregate failed: {   "errmsg" : "exception: $hour operator not accept object operand",   "code" : 16021,   "ok" : 0 } 

can me ?

just have 2 separate $project pipelines calculate field date minus 3 hours first $project pipeline stage , next pipeline step applies $hour operator on new minus 3 hours date.

suppose have following test documents inserted testing purposes:

db.test.insert([     {"created_at" : isodate("2014-06-03t02:51:42.000z")},     {"created_at" : isodate("2014-06-04t11:23:17.000z")} ]) 

the above aggregation pipeline gives hour part date minus 3 should follow:

db.test.aggregate([     {        $project:          {            _id: 0,            created_at : 1,            date_minus_3 : {$subtract:["$created_at",3*60*60*1000]},            hour_from_original: { $hour: "$created_at" }          }      },      {        $project:          {            _id: 0,            created_at : 1,            date_minus_3 : 1,            hour_from_original: 1,            hour_from_date_minus_3 : { $hour: "$date_minus_3" }          }      } ]); 

result:

/* 0 */ {     "result" : [          {             "created_at" : isodate("2014-06-03t02:51:42.000z"),             "date_minus_3" : isodate("2014-06-02t23:51:42.000z"),             "hour_from_original" : 2,             "hour_from_date_minus_3" : 23         },          {             "created_at" : isodate("2014-06-04t11:23:17.000z"),             "date_minus_3" : isodate("2014-06-04t08:23:17.000z"),             "hour_from_original" : 11,             "hour_from_date_minus_3" : 8         }     ],     "ok" : 1 } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -