Re: [mongodb-user] How to use Mongodb C API

891 views
Skip to first unread message

Wes Freeman

unread,
Aug 3, 2012, 1:20:32 PM8/3/12
to mongod...@googlegroups.com
You want to convert your query to something like this:


bson_init( query );
  bson_append_start_object( query, "$query" );
    bson_append_int( query, "age", 24 );
  bson_append_finish_object( query );

  bson_append_start_object( query, "$orderby" );
    bson_append_int( query, "float_field", 1); // or -1 for descending
  bson_append_finish_object( query );
bson_finish( query );


On Fri, Aug 3, 2012 at 12:36 PM, Tanvir Shahid <tan...@everconnect.biz> wrote:

How to orderby or sort a double field using C API?
Moreover, I am extensively using Mongo C API and I found that Mongo C API documentation is not well documented.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Gary Murakami

unread,
Aug 6, 2012, 12:46:45 PM8/6/12
to mongod...@googlegroups.com
The tutorial is a good starting place for your question.

While there is documentation available, I agree that it needs improvement so that users can
navigate with less confusion.
We welcome suggestions, the more specific the suggestions, the more likely the implementation.

In the meantime, we can try to answer you questions here.
The more specific the question, the more likely it will get a quick and specific answer.

Thanks to Wes for a timely answer.

Here's a working example that I hope helps to move you on.

setup.js
--------

    db.medals_8_6.drop();
    docs = [
        {sym: 'CHN', country: 'china', gold: 31, silver: 19, bronze: 14, total: 64, billions: 1.347},
        {sym: 'USA', country: 'United States', gold: 28, silver: 14, bronze: 19, total: 61, billions: 0.314},
        {sym: 'GBR', country: 'Great Britain', gold: 16, silver: 11, bronze: 11, total: 38, billions: 0.062},
        {sym: 'RUS', country: 'Russia', gold: 5, silver: 17, bronze: 15, total: 37, billions: 0.143},
        {sym: 'JPN', country: 'Japan', gold: 2, silver: 12, bronze: 13, total: 27, billions: 0.128}
    ];
    for (var i in docs) { db.medals_8_6.save(docs[i]); };
    //printjson(db.medals_8_6.find().toArray());

Run the above as follows:

    mongo test setup.js

mongo_c_test.c
--------------

    #include "mongo.h"
    #include <stdio.h>

    main(int argc, char** argv)
    {
        mongo conn[1];
        int status = mongo_connect(conn, "127.0.0.1", 27017);
        if (status != MONGO_OK) {
            switch(conn->err) {
                case MONGO_CONN_NO_SOCKET: printf("Socket not found\n");
                case MONGO_CONN_FAIL: printf("Connection Failed\n");
                case MONGO_CONN_NOT_MASTER: printf("Not master\n");
            }
            exit(1);
        }

        bson query[1];

        bson_init( query );
        bson_append_start_object( query, "$query" );
        bson_append_start_object( query, "total" );
        bson_append_int( query, "$gt", 1 );
        bson_append_finish_object( query );

        bson_append_finish_object( query );
        bson_append_start_object( query, "$orderby" );
        bson_append_int( query, "billions", 1); // or -1 for descending

        bson_append_finish_object( query );
        bson_finish( query );

        bson fields[1];
        bson_empty(fields);

        mongo_cursor* cursor;
        cursor = mongo_find(conn,"test.medals_8_6", query, fields, 9999, 0, 0);
        while (mongo_cursor_next(cursor) == MONGO_OK) {
            bson_print(mongo_cursor_bson( cursor ));
            printf("\t--------\n");
        }
        mongo_cursor_destroy(cursor);

        bson_destroy(query);
        mongo_destroy(conn);
        return 0;
    }

output
------

    _id : 7      501ff2a58fa20dbd483e184a
    sym : 2      GBR
    country : 2      Great Britain
    gold : 1      16.000000
    silver : 1      11.000000
    bronze : 1      11.000000
    total : 1      38.000000
    billions : 1      0.062000
    --------
    _id : 7      501ff2a58fa20dbd483e184c
    sym : 2      JPN
    country : 2      Japan
    gold : 1      2.000000
    silver : 1      12.000000
    bronze : 1      13.000000
    total : 1      27.000000
    billions : 1      0.128000
    --------
    _id : 7      501ff2a58fa20dbd483e184b
    sym : 2      RUS
    country : 2      Russia
    gold : 1      5.000000
    silver : 1      17.000000
    bronze : 1      15.000000
    total : 1      37.000000
    billions : 1      0.143000
    --------
    _id : 7      501ff2a58fa20dbd483e1849
    sym : 2      USA
    country : 2      United States
    gold : 1      28.000000
    silver : 1      14.000000
    bronze : 1      19.000000
    total : 1      61.000000
    billions : 1      0.314000
    --------
    _id : 7      501ff2a58fa20dbd483e1848
    sym : 2      CHN
    country : 2      china
    gold : 1      31.000000
    silver : 1      19.000000
    bronze : 1      14.000000
    total : 1      64.000000
    billions : 1      1.347000
    --------

Gary Murakami

unread,
Aug 8, 2012, 9:46:41 AM8/8/12
to mongod...@googlegroups.com
Has your question been answered and the issue resolved?

Tanvir Shahid

unread,
Aug 10, 2012, 9:51:59 PM8/10/12
to mongod...@googlegroups.com

Thanks Zhang..

On Thursday, August 9, 2012 12:08:34 PM UTC+9, Dan Zhang wrote:
I have same question (sort by double field). I found I must append an empty $query object before $orderby object even I just want to order it.
The code is as follows:
    ...
    bson_init( &query );
      bson_append_start_object( &query, "$query" );
      bson_append_finish_object( &query );
      
      bson_append_start_object( &query, "$orderby" );
        if (bson_append_int( &query, "distance", 1) == BSON_ERROR) {
            printf("bson_append_int error.\n");
            exit(1);
        }
      bson_append_finish_object( &query );
    bson_finish( &query );
    ...
It works now.
Regards,
Dan

在 2012年8月8日星期三UTC+8下午9时46分41秒,Gary Murakami写道:

Tanvir Shahid

unread,
Aug 11, 2012, 1:29:30 PM8/11/12
to mongod...@googlegroups.com
Hello Gary,

Following is my code:

######################################################################################
    bson_init(cond);

    bson_append_start_object(cond, "$query");

        bson_append_long(cond, "service_id", 2);

        bson_append_start_array(cond, "$or");
            bson_append_start_object(cond, "0");
                bson_append_regex(cond, "link", "^http", "");
            bson_append_finish_object(cond);
            bson_append_start_object(cond, "1");
                bson_append_regex(cond, "link", "^https", "");
            bson_append_finish_object(cond);
            bson_append_start_object(cond, "2");
                bson_append_regex(cond, "link", "^www", "");
            bson_append_finish_object(cond);
        bson_append_finish_array(cond);

        // if the data is older that 60 days
        bson_append_start_object(cond, "modified_at");
        now = time(NULL);
        now = now - (60 * 60 * 24 * 5);
        bson_append_int(cond, "$gt", (int)now);
        bson_append_finish_object(cond);

    bson_append_finish_object(cond);

    bson_append_start_object(cond, "$orderby");
    bson_append_int(cond, "modified_at", 1); // or -1 for descending
    bson_append_finish_object(cond);

    bson_finish(cond);

    int num;

    num = mongo_count(conn, "test", "web_collection", cond);
    printf("%d\n", num);
################################################################

The above program print 0 but if I remove following 3 lines:

bson_append_start_object(cond, "$orderby");
bson_append_int(cond, "modified_at", 1); // or -1 for descending
bson_append_finish_object(cond);

Then it will return 104. I think it's wrong.

My mongodb build info is as follows:

version2.0.7
gitVersion875033920e8869d284f32119413543fa475227bf
sysInfoLinux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_41
versionArrayArray
bits32
debug
maxBsonObjectSize16777216
 
I would like to get your advice in this scenario.

Cheers,
Tanvir

Gary Murakami

unread,
Aug 14, 2012, 5:10:18 PM8/14/12
to mongod...@googlegroups.com
Tanvir:

mongo_count is a special command that does a fast count on the server.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-{{count%28%29}}

Since you are just counting, there's no reason to specify sort order,
and similarly there's no reason for skip and limit.

The BSON syntax is also different for the C driver.
The other language drivers don't expose $query and friends for find,
and have additional special clauses or options for sort, skip, and limit.

The following works for me.  Please give it a try and see if it works for you.

Blessings,

-Gary

    /*
        bson_append_start_object(cond, "$query");
    */

            bson_append_long(cond, "service_id", 2);

            bson_append_start_array(cond, "$or");
                bson_append_start_object(cond, "0");
                    bson_append_regex(cond, "link", "^http", "");
                bson_append_finish_object(cond);
                bson_append_start_object(cond, "1");
                    bson_append_regex(cond, "link", "^https", "");
                bson_append_finish_object(cond);
                bson_append_start_object(cond, "2");
                    bson_append_regex(cond, "link", "^www", "");
                bson_append_finish_object(cond);                                                 Tanvir

            bson_append_finish_array(cond);

            // if the data is older that 60 days
            bson_append_start_object(cond, "modified_at");
            now = time(NULL);
            now = now - (60 * 60 * 24 * 5);
            bson_append_int(cond, "$gt", (int)now);
            bson_append_finish_object(cond);
    /*

        bson_append_finish_object(cond);

        bson_append_start_object(cond, "$orderby");
        bson_append_int(cond, "modified_at", 1); // or -1 for descending
        bson_append_finish_object(cond);
    */

        bson_finish(cond);

        int num;

        num = mongo_count(conn, "test", "web_collection", cond);
        printf("%d\n", num);




Reply all
Reply to author
Forward
0 new messages