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 8632979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:25:33+00:00 2026-06-12T09:25:33+00:00

I have a database in MongoDb that contains two collections: ‘categories’ and ‘articles’. I’m

  • 0

I have a database in MongoDb that contains two collections: ‘categories’ and ‘articles’.
I’m using Mongoose on NodeJs to connect to the database and read the categories. I want to calculate the number of articles for a category without making an additional request/query, so it would be perfect if I could solve this at the database level.

An item from the ‘categories’ collection looks like:

{
'_id' : ObjectId("..."),
'feed_id' : 1,
'name': 'Blog posts'
}

An item from the ‘articles’ collection looks like:

{
'_id' : ObjectId("..."),
'feed_id' : 1,
'title': 'Article title',
'published' : '12/09/2012',
...
}

so the categories and articles are linked using the ‘feed_id’ field.

I would like to export all categories together with a corresponding number of articles:

{
'_id' : ObjectId("..."),
'feed_id' : 1,
'name': 'Blog posts',
'no_articles': 4
}

I’m not sure how exactly I should do this:

1) Create a ‘no_articles’ field in the categories collection? If yes, I would like this to be updated automatically when a document is inserted or deleted from the articles collection.

2) Sum up the articles into ‘no_articles’ when categories are read?

I read something about MapReduce and group, but didn’t quite understand if it’s possible to use them for this particular task.

  • 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-12T09:25:33+00:00Added an answer on June 12, 2026 at 9:25 am

    This is one of use cases where traditional relational databases really shine.

    It is impossible to do that with one query in mongodb. The “no_articles field” you mentioned is the way to go. Common name (among Rails people, anyway) for this approach is: Counter Cache Column. I am not very familiar with Mongoose, so I don’t know whether it will maintain that field for you or not. MongoDB itself certainly won’t do it. But maintaining it yourself isn’t a lot of work, you just need to be accurate.

    I advise against counting articles when you read categories. This is a classic example of N+1 query problem and counter cache column is there to prevent it.

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

Sidebar

Related Questions

I am writing a Happstack server and I have a MongoDB database to connect
I have a bunch of collections in my MongoDB database. To illustrate what I'm
I'm trying to connect to a MongoDB database that requires a password while also
I have an Expressjs app running on NodeJS 0.8.8 using MongoDB and the Jade
I have a meteor app that is feeding data in a MongoDB database to
I have a system that stores data into a MongoDB database. Now I need
Good day, i have mongodb database filled with some data, i ensured that data
I am using pymongo to connect to my mongodb database. I am trying to
I have a MongoDB database with documents that look like this: {users : [u1,
i have database with two values id and val so i want to ask

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.