1

今日が誕生日のユーザーを返す MongoDB クエリを作成しているので、$where ステートメントで次の JS 関数を作成しました。

function() {
    if (! this.birthday.getMonth) {return false;}

    return this.birthday.getMonth() == 1
    && this.birthday.getDate() == 1
}

残念ながら、誕生日フィールドがドキュメントに設定されていない場合、エラーが発生するため、if ステートメントは機能していないようです。

JS Error: TypeError: this.birthday has no properties nofile_18

どんな助けでも大歓迎です。

4

2 に答える 2

0

誕生日フィールドが日付型または ISODate 型として格納されていると仮定すると、集計フレームワークを使用して正しい比較を行うことができます。javascript シェルを起動する $where よりも高速になると思います。

// construct "thisMonth" variable which is current month as a number
// and thisDay which is current date of the month as a number
> var thisMonth = 5;
> var thisDay = 1;
> db.people.aggregate( [ 
        {$project:{month:{$month:"$birthday"}, date:{$dayOfMonth:"$birthday"}}},
        {$match: {month:thisMonth, date: thisDay}}
  ] )
于 2013-05-01T17:40:13.120 に答える
0

$where ステートメントの前に$existsを追加して、ようやく問題を解決しました。「誕生日」フィールドがない場合でも、正常に機能するようになりました。

于 2013-05-02T10:11:06.923 に答える