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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:39:14+00:00 2026-05-20T18:39:14+00:00

I’m trying to make a MongoDB database for a simple voting system. If I

  • 0

I’m trying to make a MongoDB database for a simple voting system. If I were to draw a schema, then it looks something like this:

User {
    name: String
  , email: String
}

Vote {
    message: String
  , voters: [ObjectId(User._id)]
}

I have some questions about this design when there are a lot of voters for one vote:

  1. Sending the whole voters array to the client’s side (not to mention caching it in memory) is very expensive, right? Is there a way to get the Vote in a “shallow” way, so when I need vote.voters, it will make another database request to the array of voters?
  2. If a voter has voted already, I want to check that and not count his vote. To do that, is there a query I can run in the array of embedded voters to quickly find this?
  3. When showing votes, I’d want to show the number of votes without fetching the voters array to the client side. Is there some kind of count query I can run to count the voters length?
  • 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-05-20T18:39:15+00:00Added an answer on May 20, 2026 at 6:39 pm

    I would add a bit of redundancy to the schema to avoid some of the potential problems you mention. I assume you want to 1) quickly count the number of votes and 2) make sure a user cannot vote twice.

    One way to achieve this is to keep both a list of users and a count of votes, and add a clause to the update query that makes sure that a vote is only added if the user’s ID is not in the list of voters:

    var query = {_id: xyz, voters: {$ne: user_id}
    var update = {$inc: {votes: 1}, $push: {voters: user_id}}
    db.votes.update(query, update, true)
    

    (the last parameter is upsert, a very nice feature of Mongo)

    Then, if you want to show the number of votes you can limit the properties of the result to the votes property:

    db.votes.find({_id: xyz}, {votes: true})
    

    You can find a complete description of more or less exactly what you want to do here: http://cookbook.mongodb.org/patterns/votes/

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

Sidebar

Related Questions

No related questions found

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.