Mongodb查询特定月份|年份不是日期 [英] Mongodb query specific month|year not date

查看:54
本文介绍了Mongodb查询特定月份|年份不是日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 mongodb 中查询特定月份,而不是日期范围,我需要月份来制作当月客户生日列表.

How can I query a specific month in mongodb, not date range, I need month to make a list of customer birthday for current month.

在 SQL 中将是这样的:

In SQL will be something like that:

SELECT * FROM customer WHERE MONTH(bday)='09'

现在我需要在 mongodb 中翻译它.注意:我的日期已经保存在 MongoDate 类型中,我以前用这个想法很容易工作,但现在我找不到如何做这个简单的事情.

Now I need to translate that in mongodb. Note: My dates are already saved in MongoDate type, I used this thinking that will be easy to work before but now I can't find easily how to do this simple thing.

推荐答案

你可以使用 aggregate$month 投影运算符:

You can do that using aggregate with the $month projection operator:

db.customer.aggregate([
  {$project: {name: 1, month: {$month: '$bday'}}},
  {$match: {month: 9}}
]);

这篇关于Mongodb查询特定月份|年份不是日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆