레이블이 mongoc_collection_aggregate인 게시물을 표시합니다. 모든 게시물 표시
레이블이 mongoc_collection_aggregate인 게시물을 표시합니다. 모든 게시물 표시

2015년 6월 19일 금요일

Aggregation Framework Examples at MongoDB with C

출처: http://api.mongodb.org/c/current/aggregate.html

Aggregation Framework Examples


#include <mongoc.h>
#include <stdio.h>

static void
print_pipeline (mongoc_collection_t *collection)
{
   mongoc_cursor_t *cursor;
   bson_error_t error;
   const bson_t *doc;
   bson_t *pipeline;
   char *str;

   pipeline = BCON_NEW ("pipeline", "[",
      "{", "$group", "{", "_id", "$state", "total_pop", "{", "$sum", "$pop", "}", "}", "}",
      "{", "$match", "{", "total_pop", "{", "$gte", BCON_INT32 (10000000), "}", "}", "}",
   "]");

   cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);

   while (mongoc_cursor_next (cursor, &doc)) {
      str = bson_as_json (doc, NULL);
      printf ("%s\n", str);
      bson_free (str);
   }

   if (mongoc_cursor_error (cursor, &error)) {
      fprintf (stderr, "Cursor Failure: %s\n", error.message);
   }

   mongoc_cursor_destroy (cursor);
   bson_destroy (pipeline);
}

int
main (int argc,
      char *argv[])
{
   mongoc_client_t *client;
   mongoc_collection_t *collection;

   mongoc_init ();

   client = mongoc_client_new ("mongodb://localhost:27017");
   collection = mongoc_client_get_collection (client, "test", "zipcodes");

   print_pipeline (collection);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);

   mongoc_cleanup ();

   return 0;
}





2015년 6월 18일 목요일

MongoDB에서 최고값 가져오기 with C (SQL의 MAX와 동일)

최고값 구하는거때매 한참 해맸네. 뭔가 지원되겠거니 편해볼라고 기대한 내 잘못이지;;

case SQL:
SELECT MAX(sid) FROM {TABLE NAME};

case MongoDB:
 
#include <bson.h>
#include <mongoc.h>

#define MGDOID "sid"

// MongoDB에서 최고값 가져오기
unsigned long GetMaxFromMongoDB()
{
 mongoc_client_t *client;
 mongoc_collection_t *collection;
 mongoc_cursor_t *cursor;

 bson_t *pipeline;
 bson_iter_t iter;
 const bson_t *doc;
 const bson_value_t *value;

 unsigned long ans;
 
 // for debug
 clock_t cStartClock;
 cStartClock = clock();

 //mongoDB 초기화
 mongoc_init();
 client = mongoc_client_new(MONGO_SERVER);

 if (!client) {
  fprintf(stderr, "[MongoDB] Failed to parse URI.\n");
  return 0;
 }

  //MongoDB - Collection 연결
 collection = mongoc_client_get_collection (client, MONGO_DBNAME, MONGO_COLLECTION);

 pipeline = BCON_NEW ("pipeline", "[",
      "{", "$project", "{", MGDOID, BCON_INT32(1), "}", "}",
      "{", "$sort", "{", MGDOID, BCON_INT32(-1), "}", "}",
   "{", "$limit", BCON_INT32(1), "}",
   "]");

 cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
 
 while (mongoc_cursor_next(cursor, &doc)) {
  if(bson_iter_init(&iter, doc)) {
   while (bson_iter_next(&iter)) {
    if( strncmp(bson_iter_key(&iter), MGDOID, 3) == 0) {
     value = bson_iter_value (&iter);
//fprintf(stderr, "[%" PRId64 "]\n", value->value.v_int64);
     ans = (long)value->value.v_int64;
    }
   }
  }
 }
 bson_destroy(pipeline);

 mongoc_collection_destroy (collection);
 mongoc_client_destroy (client);
 mongoc_cleanup();

 cStartClock = (clock() - cStartClock);
 fprintf(stderr, "> GET MAX ID From MongoDB[%ld]: %2.8f sec.\n", ans, cStartClock / (double)CLOCKS_PER_SEC);

 return ans;
}