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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:16:14+00:00 2026-06-16T03:16:14+00:00

In the following Mongodb query I’m getting undefined in my $divide expression for correct

  • 0

In the following Mongodb query I’m getting “undefined” in my $divide expression for “correct” and “guessCount”. How do I correct this? My intent is to return a percentage labeled as “accuracy” that is the result of dividing the sum of correct guesses divided by the number of guesses. This should be pretty clear from the provided code.

guessCollection.aggregate( { $group :
                     { _id : "$imageFileName",
                       correct : { $sum : "$correct" },
                       guessCount: {$sum: 1 },
                       accuracy: {$divide: [correct, guessCount]}, //guess and guessCount are undefined
                        } },
                   { $match : {guessCount : { $gte : 50 }, user: user, language: language } } ,
                   { $sort: { guessCount: -1 } }, callback)
  • 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-16T03:16:19+00:00Added an answer on June 16, 2026 at 3:16 am

    $divide is a $project operator, not a $group operator, so you need to do this instead:

    guessCollection.aggregate( { $group :
        { _id : "$imageFileName",
           correct : { $sum : "$correct" },
           guessCount: {$sum: 1 }
        } },
        { $match : {guessCount : { $gte : 50 }, user: user, language: language } } ,
        { $project : {
            correct: 1,
            guessCount: 1,
            accuracy: {$divide: ['$correct', '$guessCount']}
        }},
        { $sort: { guessCount: -1 } }, callback)
    

    Your next problem will likely be that you’re referencing the language and user fields in your $match because you’re not including those in your $group so you’ll probably want to move that part to the beginning of your pipeline like this:

    guessCollection.aggregate(
        { $match : { user: user, language: language } } ,
        { $group : { 
            _id : "$imageFileName",
            correct : { $sum : "$correct" },
            guessCount: {$sum: 1 }
        } },
        { $match : {guessCount : { $gte : 50 } } } ,
        { $project : {
            correct: 1,
            guessCount: 1,
            accuracy: {$divide: ['$correct', '$guessCount']}
        }},
        { $sort: { guessCount: -1 } }, callback)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I perform the following query in MongoDB shell, all is fine: db.users.find({location: {$near:
I am using the MongoDB PHP Library and have the following query array Array
I'm following this example for creating a simple Web application using JAX-RS, MongoDB and
Consider the following (rather complicated) query expressed in this JSON object: { name: Kindle
I am trying to run the following code in PHP to query a MongoDB:
Is it possible to express the following SQL query in mongodb: SELECT * FROM
I'm using mongodb and I've built to make the following query. 1 AccessToken.where('resource_owner_id').equals(event.resource_owner_id) 2
I ran the following query on a collection in my mongodb database. db.coll.find({field_name:{$exists:true}}).count() and
I need to query the following data from mongodb: Project has many Regions, a
I have set up MongoDB following this tutorial http://www.littlelostmanuals.com/2011/09/spring-mongodb-type-safe-queries.html Everything works as expected but

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.