Monday 15 June 2015

mongoose - Query Mongodb on specific or range month without year of a datetime -



mongoose - Query Mongodb on specific or range month without year of a datetime -

i'm using mongodb store datetime.

example

db data:

[ { id: 1 date: '2014-11-01t00:00:00.000z' }, { id: 2 date: '2013-10-11t00:00:00.000z' }, { id: 3 date: '2012-11-21t00:00:00.000z' } ]

filter month 11, query.find({'date': {$month: 11}}) should result:

[ { id: 1 date: '2014-11-01t00:00:00.000z' }, { id: 3 date: '2012-11-21t00:00:00.000z' } ]

how query want?

i search , got query mongodb on month, day, year... of datetime, it's not want, don't want filter year of date.

$month aggregation operator, can used aggregate, not find:

class="lang-javascript prettyprint-override">db.test.aggregate([ {$project: { id: 1, date: 1, month: {$month: '$date'} }}, {$match: {month: 11}}, {$project: { id: 1, date: 1 }} ])

result:

class="lang-javascript prettyprint-override">{ "result" : [ { "_id" : objectid("5453d0b73712b66352662484"), "id" : 1, "date" : isodate("2014-11-01t00:00:00.000z") }, { "_id" : objectid("5453d0b73712b66352662486"), "id" : 3, "date" : isodate("2012-11-21t00:00:00.000z") } ], "ok" : 1 }

mongodb mongoose

No comments:

Post a Comment