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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:03:11+00:00 2026-06-14T19:03:11+00:00

I have a data. Let me show you the example of that 1 1

  • 0

I have a data. Let me show you the example of that

1
1
2
2
2
3

And, I’d like to get the number of each values using Group by. The result will be like below

value | count
1 | 2
2 | 3
3 | 1

Maybe, the group by query on Mongo Shell is like this

db.collection.group(
{ key : {value:true},
reduce: function(obj, prev) { prev.csum += 1; },
initial: { csum: 0 }
})

But, I have to convert that query to C code with mongoDB C API.
I tried to make some code using bson_append_bson like that.. but, failed..

What shoould I do?

  • 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-14T19:03:13+00:00Added an answer on June 14, 2026 at 7:03 pm

    I setup a Mongo 2.2.1 instance on my machine on port 12345. I created a test.data collection and populated it with

    use test
    db.data.insert({value:1})
    db.data.insert({value:1})
    db.data.insert({value:2})
    db.data.insert({value:2})
    db.data.insert({value:2})
    db.data.insert({value:3})
    

    per your sample data. Using the latest driver, I wrote the following sample code.

    #include <stdio.h>
    #include <mongo.h>
    
    int main( char *argv[] ) {
        bson b, out;
        mongo conn;
    
        if( mongo_connect( &conn, "127.0.0.1", 12345 ) != MONGO_OK ) {
            switch( conn.err ) {
                case MONGO_CONN_NO_SOCKET:
                    printf( "FAIL: Could not create a socket!\n" );
                    break;
                case MONGO_CONN_FAIL:
                    printf( "FAIL: Could not connect to mongod. Make sure it's listening at 127.0.0.1:27017.\n" );
                    break;
                }
    
            exit( 1 );
        }
    
        bson_init( &b );
            bson_append_string( &b, "aggregate", "data" );
            bson_append_start_array( &b, "pipeline" );
                bson_append_start_object( &b, "0" );
                    bson_append_start_object( &b, "$group" );
                        bson_append_string( &b, "_id", "$value" );
                        bson_append_start_object( &b, "count" );
                            bson_append_int( &b, "$sum", 1 );
                        bson_append_finish_object( &b );
                    bson_append_finish_object( &b );
                bson_append_finish_object( &b );
            bson_append_finish_array( &b );
        bson_finish( &b );
        bson_print( &b );
    
        printf( "command result code: %d\n", mongo_run_command( &conn, "test", &b, &out ) );
        bson_print( &out );
    
        bson_destroy( &b );
        bson_destroy( &out );
        mongo_destroy( &conn );
        return( 0 );
    }
    

    Instead of the $group command, which doesn’t work in sharded environments, I used the Aggregation Framework (new since 2.1). The above C code is the equivalent to

    use test
    db.data.aggregate({$group:{_id:"$value",count:{$sum:1}}})
    

    in the Mongo shell.

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

Sidebar

Related Questions

I have a large nested vector that look like this: import Data.Vector let x
I have some data, let's say the number of sells of the month. I
Let's assume that we have a data frame x which contains the columns job
Let's say i have a database design like this: for the data: id |
In a webapp, i have a data-model (let's call it Item). The data of
Let say if I have this data my_data = [ 10 20 30 40;
Let say I have the following data in R. training = factor(c(1,1,3,2,1,3,2,34,67,34)) test =
Let's say i have this json data. How to transform the tags to a
Let's say I have some simple data y = [[datetime.datetime( 2012,1,1,1,1), 2.1], [datetime.datetime( 2012,1,1,1,2),
Let's say I have two strings: one is XML data and the other is

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.