2016-12-09 11 views
3

私は現在の時刻よりも小さい$$item.dateのドキュメントを取得するだけの日付比較の以下の集計を持っています。私は上記についての質問があり

[ 
    { $match : { "_id" : "57b4c5f0291ebb13110b888e" } }, 
    { $project : { 
      "fulfillments" : { 
       $filter : { 
        "input" : "$fulfillments", 
        "as" : "item", 
        "cond" : { "$gte" : ["$$item.date","new Date()"]} 
       } 
      } 
     } 
    }, 

    ... 

    ... 
] 

重要な部分は、以下の通りである。

"cond" : { "$gte" : ["$$item.date","new Date()"]}

これは私が変更できるよう動作していないようですnew Date()から1または0またはかなり多くの価値があり、それでもすべての文書が返されます。現在の日付以上のdateのドキュメントのみを返す必要があります。例えば

、それは2017-11-23

{ 
     "_id": "582deb33bdf17300010a2ae5", 
     "date": new Date("2017-11-23T17:00:00-0700"), 
    } 

であるため、次の文書

{ 
    "_id": "57b4c5f0291ebb13110b888e", 
    "fulfillments": [ 
    { 
     "_id": "582deb33bb117300010a2ae5", 
     "date": new Date("2016-11-23T17:00:00-0700"), 
    }, 
    { 
     "_id": "582deb33bdf17300010a2ae5", 
     "date": new Date("2017-11-23T17:00:00-0700"), 
    } 
} 

のみ以下fulfillmentが返されるべきである与えられた更新 私は正確な文書を与えている場合は質問がありますstrucutreので、私はこれを検証するためのスクリーンショットを含めた。

enter image description here

+0

については以下を参照、クエリから"を削除。 – Veeram

+0

@Veeram私は引用符なしで試してみると、少なくともIDEとゴランでは、その必要があります。 – TheJediCowboy

+0

'new Date()'の引用符を削除してください: '{" $ gte ":[" $ item.date "、new Date()]}' – styvane

答えて

1

あなただけのマシン上で現在の時刻をしたい場合は、単に私が問題になる新しい日付の周りにその引用符を考える私の例

> db.test.find().pretty() 
{ 
     "_id" : "57b4c5f0291ebb13110b888e", 
     "fulfillments" : [ 
       { 
         "_id" : "582deb33bb117300010a2ae5", 
         "date" : ISODate("2016-11-24T00:00:00Z") 
       }, 
       { 
         "_id" : "582deb33bdf17300010a2ae5", 
         "date" : ISODate("2017-11-24T00:00:00Z") 
       } 
     ] 
} 
> 
> db.test.aggregate([ 
...  { $match : { "_id" : "57b4c5f0291ebb13110b888e" } }, 
...  { $project : { 
...    "fulfillments" : { 
...     $filter : { 
...      "input" : "$fulfillments", 
...      "as" : "item", 
...      "cond" : { "$gte" : ["$$item.date",new Date()]} 
...     } 
...    } 
...   } 
...  } 
... ]).pretty() 
{ 
     "_id" : "57b4c5f0291ebb13110b888e", 
     "fulfillments" : [ 
       { 
         "_id" : "582deb33bdf17300010a2ae5", 
         "date" : ISODate("2017-11-24T00:00:00Z") 
       } 
     ] 
} 
>