0

I have the following sample data:

var data = [{
    "_id" : ObjectId("583f6e6d14c8042dd7c979e6"),
    "transid" : 1,
    "acct" : "acct1",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category1",
    "amount" : 103 
}, {
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 2,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category2",
    "amount" : 103 
}, {
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 3,
    "acct" : "acct2",
    "transdate" : ISODate("2016-07-31T05:00:00.000Z"),
    "category" : "category1",
    "amount" : 103 
}]

How do I run a query to return all documents (all fields) where the year of tansdate = 2016 and the month of transdate = 7 (july)?

I would like to know how to do it in both simple mongodb query syntax based on the $year and $month syntax.

mo_maat
  • 2,110
  • 12
  • 44
  • 72
  • When you ask a question on StackOverflow there is no such thing as "also". This makes it "more than one question" and that means [Ask a new Question](https://stackoverflow.com/questions/ask) instead. "Date Range" queries are common and basically apply to **anything**, be it over a year or over a day or a few minutes. It's all a "range". If you want to query by "month" that is **independent** of the year, then you "should" in fact store that as a separate field, for best performance. But a "given month **and** year", that's a "range". – Neil Lunn Aug 01 '17 at 05:10
  • I see regarding the also. I have been using the range syntax, but I wanted to know if there is a way to use the $month and $year syntax kind of like in sql to get the dates that match. As for the update question I'll edit to remove it. The duplicate question I was pointed to does not answer my question. I want to know for learning purposes. – mo_maat Aug 01 '17 at 05:13
  • No there is not. Not in any efficient way anyhow, and actually the same is true in SQL. Just because you did it there does not mean it's "good practice". Which is exactly why when establishing things in other non-relational products, we try to "stamp out" such bad practices. Use ranges. They actually use indexes. Calculations do not. – Neil Lunn Aug 01 '17 at 05:16
  • Makes sense. I do get too that it is not the most efficient, but again, for the sake of learning I just wanted to know. I am already using ranges. But thanks. I appreciate the effort to instill good habits. – mo_maat Aug 01 '17 at 05:30
  • And so what would proper protocol be in this case, delete the question, mark it answered or just leave it as is? – mo_maat Aug 01 '17 at 05:31
  • Leave it. Worst case it's gets downvoted and then you can either delete it or it gets automatically deleted. Best case, other people search for the same title and it points them to the correct answers in the marked duplicate. But do whatever you want really. – Neil Lunn Aug 01 '17 at 05:33
  • Got it. Thanks for the help and pointers. I'll leave as it could perhaps benefit someone. – mo_maat Aug 01 '17 at 07:02

0 Answers0