Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:17:07+00:00 2026-06-12T05:17:07+00:00

I have a collection full of documents with a created_date attribute. I’d like to

  • 0

I have a collection full of documents with a created_date attribute. I’d like to send these documents through an aggregation pipeline to do some work on them. Ideally I would like to filter them using a $match before I do any other work on them so that I can take advantage of indexes however I can’t figure out how to use the new $year/$month/$dayOfMonth operators in my $match expression.

There are a few examples floating around of how to use the operators in a $project operation but I’m concerned that by placing a $project as the first step in my pipeline then I’ve lost access to my indexes (MongoDB documentation indicates that the first expression must be a $match to take advantage of indexes).

Sample data:

{
    post_body: 'This is the body of test post 1',
    created_date: ISODate('2012-09-29T05:23:41Z')
    comments: 48
}
{
    post_body: 'This is the body of test post 2',
    created_date: ISODate('2012-09-24T12:34:13Z')
    comments: 10
}
{
    post_body: 'This is the body of test post 3',
    created_date: ISODate('2012-08-16T12:34:13Z')
    comments: 10
}

I’d like to run this through an aggregation pipeline to get the total comments on all posts made in September

{
    aggregate: 'posts',
    pipeline: [
         {$match:
             /*Can I use the $year/$month operators here to match Sept 2012?
             $year:created_date : 2012,
             $month:created_date : 9
             */
             /*or does this have to be 
             created_date : 
                  {$gte:{$date:'2012-09-01T04:00:00Z'}, 
                  $lt: {$date:'2012-10-01T04:00:00Z'} }
             */
         },
         {$group:
             {_id: '0',
              totalComments:{$sum:'$comments'}
             }
          }
    ]
 }

This works but the match loses access to any indexes for more complicated queries:

{
    aggregate: 'posts',
    pipeline: [
         {$project:
              {
                   month : {$month:'$created_date'},
                   year : {$year:'$created_date'}
              }
         },
         {$match:
              {
                   month:9,
                   year: 2012
               }
         },
         {$group:
             {_id: '0',
              totalComments:{$sum:'$comments'}
             }
          }
    ]
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:17:08+00:00Added an answer on June 12, 2026 at 5:17 am

    As you already found, you cannot $match on fields that are not in the document (it works exactly the same way that find works) and if you use $project first then you will lose the ability to use indexes.

    What you can do instead is combine your efforts as follows:

    {
        aggregate: 'posts',
        pipeline: [
             {$match: {
                 created_date : 
                      {$gte:{$date:'2012-09-01T04:00:00Z'}, 
                      $lt:  {date:'2012-10-01T04:00:00Z'} 
                      }}
                 }
             },
             {$group:
                 {_id: '0',
                  totalComments:{$sum:'$comments'}
                 }
              }
        ]
     }
    

    The above only gives you aggregation for September, if you wanted to aggregate for multiple months, you can for example:

    {
        aggregate: 'posts',
        pipeline: [
             {$match: {
                 created_date : 
                      { $gte:'2012-07-01T04:00:00Z', 
                        $lt: '2012-10-01T04:00:00Z'
                      }
             },
             {$project: {
                  comments: 1,
                  new_created: {
                            "yr" : {"$year" : "$created_date"},
                            "mo" : {"$month" : "$created_date"}
                         }
                  }
             },
             {$group:
                 {_id: "$new_created",
                  totalComments:{$sum:'$comments'}
                 }
              }
        ]
     }
    

    and you’ll get back something like:

    {
        "result" : [
            {
                "_id" : {
                    "yr" : 2012,
                    "mo" : 7
                },
                "totalComments" : 5
            },
            {
                "_id" : {
                    "yr" : 2012,
                    "mo" : 8
                },
                "totalComments" : 19
            },
            {
                "_id" : {
                    "yr" : 2012,
                    "mo" : 9
                },
                "totalComments" : 21
            }
        ],
        "ok" : 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have documents collection Messages in my base (RavenDB) Document definition like: class Message
Lets say I have collection of documents with specified longitude and latitude. type is
I have a collection with nested documents in it. Each document also has an
In MongoDB I have a collection of documents called 'clients', where each document is
I have a large collection of documents scanned into PDF format, and I wish
I am using mongoid for rails (latest version). I have a collection full of
I have Collection List<Car> . How to compare each item from this collection with
I'm planning to have collection of items stored in a TCollection. Each item will
I have Class and Student objects. Both have collection of another as property. Which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.