Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

Mongodb query specific month|year not date

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.

In SQL will be something like that:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With MongoDB 3.6 and newer, you can use the $expr operator in your find() query. This allows you to build query expressions that compare fields from the same document in a $match stage.

db.customer.find({ "$expr": { "$eq": [{ "$month": "$bday" }, 9] } })

For other MongoDB versions, consider running an aggregation pipeline that uses the $redact operator as it allows you to incorporate with a single pipeline, a functionality with $project to create a field that represents the month of a date field and $match to filter the documents which match the given condition of the month being September.

In the above, $redact uses $cond tenary operator as means to provide the conditional expression that will create the system variable which does the redaction. The logical expression in $cond will check for an equality of a date operator field with a given value, if that matches then $redact will return the documents using the $$KEEP system variable and discards otherwise using $$PRUNE.

Running the following pipeline should give you the desired result:

db.customer.aggregate([
    { "$match": { "bday": { "$exists": true } } },
    {
        "$redact": {
            "$cond": [
                { "$eq": [{ "$month": "$bday" }, 9] },
                "$$KEEP",
                "$$PRUNE"
            ]
        }
    }
])

This is similar to a $project +$match combo but you'd need to then select all the rest of the fields that go into the pipeline:

db.customer.aggregate([
    { "$match": { "bday": { "$exists": true } } },
    {
        "$project": {
            "month": { "$month": "$bday" },
            "bday": 1,
            "field1": 1,
            "field2": 1,
            .....
        }
    },
    { "$match": { "month": 9 } }
])

With another alternative, albeit slow query, using the find() method with $where as:

db.customer.find({ "$where": "this.bday.getMonth() === 8" })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...