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
--------