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

查看:552
本文介绍了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.

推荐答案

您可以在

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天全站免登陆