-1
我想從一個日期字段設置時間以當天我如何操縱聚合管道中的日期字段?
function getDate(date){ return new Date(date.getYear(), date.getMonth(),
date.getDate(), 0,0,0); }
...
{"$project" : {
"_id" : getDate("$dt"),
...
的開始。如果我送
"$dt"
我得到TypeError: date.getYear is not a function
爲我傳遞一個字符串,如果刪除引號,我得到
Error: "$dt is not defined"
,- 但如果我將
"$dt"
設置爲"_id"
的值,我會得到正確的ISO日期。
那麼,如何通過Date對象的功能?
字符串中沒有'getYear'方法... – Li357
我認爲您正在尋找['$ year'](https://docs.mongodb.com/manual/reference/operator/aggregation/year/ ) 這裏。什麼是混淆你,我覺得是一樣的東西'{「$ GT」,新的日期()}'實際上並沒有被髮送到服務器,而是「價值」被退回「之前」的請求被髮送到服務器,然後使用。您無法將JavaScript函數發送到聚合管道或查詢中。除了'$ where'外,實際上它實際上是發送一個「字符串」。 –
@AndrewLi如何傳遞的日期對象的功能呢?如果我刪除引號,我得到「$ DT沒有定義」錯誤,但如果我設置爲「$ DT」爲「_id」值,我得到一個正確的ISO日期。 – Dane411