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

  • SEARCH
  • Home
  • 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 9275299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:33:21+00:00 2026-06-18T16:33:21+00:00

I feel like I must be missing something obvious. Here is the aggregation, as

  • 0

I feel like I must be missing something obvious. Here is the aggregation, as one would post it in the shell:

db.documents.aggregate(
   { $project: { title: 1, "date.year": 1, decade:
   { $subtract: ['$date.year', { $mod: ['$date.year', 10]}]}
}})

This is supposed to take a list of documents, each with a date.year field, and add a decade field indicating which decade the document is in (1900, 1910, etc.) I’m planning on further transforming the data after I get that added.

The problem is that when I run the aggregation, I get:

{
    "errmsg" : "exception: $subtract resulted in a non-numeric type",
    "code" : 16413,
    "ok" : 0
}

If I change $subtract to $add, it works fine (but doesn’t give me the right result, of course.) So what’s going on with the subtraction? Why am I getting a non-numeric type when I subtract but a number when I add?

Thanks in advance!

  • 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-18T16:33:23+00:00Added an answer on June 18, 2026 at 4:33 pm

    This looks like a bug in the aggregation framework – it’s not handling subtraction correctly when the fields you are operating for are not set in the documents going through the pipeline.

    It’s been fixed in 2.3.2 (I can’t reproduce this – it projects “null” when “date” is not set) but one way you can work around this limitation is by adding a $match condition to your pipeline, i.e. prefix {$project} with:

    {$match: {"date.year":{$exists:true}}}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I must be missing something completely obvious, but I don't see
I feel like I must be missing something simple here... Nothing sends, nor do
I feel like I'm missing something obvious, but I can't work out why my
I feel silly asking this, I must be missing something obvious. My javascript looks
I feel like I'm missing something obvious. I've got a custom segue class. It
I feel like I must be missing something, but the git issues README doesn't
I feel like I must be missing a keyword or something when searching since
I feel like I must be missing something, but I just don't see what
I feel like I must be missing something so simple as this form field
I'm feeling very, very stupid right now... I feel like I must be missing

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.