0
mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0);

  count_matched=0;    
  bson *doc;

  while(mongo_cursor_next(cursor)==MONGO_OK)
  {   
      count_matched++;

      doc=(bson *)mongo_cursor_bson(cursor);
      bson_iterator_init(&it,doc);

   while(bson_iterator_next(&it) != BSON_EOO)
   {
       fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it));

   }
  }

This code is working perfectly and i can see the matched documents (Key + Value) but now i want to save the matched document's key and value to a string. Can any tell me how i can save the return value of key and value in to a string?

One document includes (all strings)

Total Key=10
Total value=10

and i want to save 10 document's key and value at one time. I am using C driver of mongodb.

2
  • Can you use sprintf? Commented Dec 17, 2013 at 13:17
  • I added below my answer, can you please check it and let me know it validity..its working properly in my environment..or if you can give some other suggestions to utilize it. Thanks Commented Dec 17, 2013 at 13:44

2 Answers 2

1

The following code shows how you would be doing copy of the key and values from the bson iterator into your key-value arrays temp_key and temp_value. The specific block of code is in between the comments marked START and END.

Additionally, you can find documentation for accessing BSON document contents at http://api.mongodb.org/c/current/bson.html .

mongo_cursor *cursor = mongo_find(&conn, TEST_NS, &query, NULL, 0, 0, 0);
int count_matched = 0;
bson *doc;

// Assuming you are just looking for 100 key / value pair of max length of 99 characters
const unsigned KV_ARRAY_LENGTH = 100;
const unsigned MAX_KV_LENGTH = 105;
char temp_key[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1], temp_value[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1];
int i = 0;
while (mongo_cursor_next(cursor) == MONGO_OK) {
    count_matched++;
    doc=(bson *)mongo_cursor_bson(cursor);

    bson_iterator it;
    bson_iterator_init(&it,doc);

    while (bson_iterator_next(&it) != BSON_EOO) {
        fprintf(stderr,"%s : %s\n", bson_iterator_key(&it), bson_iterator_string(&it));

        /******* START - Code to capture key-value into appropriate array */
        if (i < KV_ARRAY_LENGTH) {
            /* - Collect key-value pairs only if there is space in the array 
             * - Key / Value woud be captured only till the max amount of space available for them i.e. MAX_KV_LENGTH in this case
             * */
            strncpy(temp_key[i], bson_iterator_key(&it), MAX_KV_LENGTH);
            strncpy(temp_value[i], bson_iterator_string(&it), MAX_KV_LENGTH);
            temp_key[i][MAX_KV_LENGTH] = temp_value[i][MAX_KV_LENGTH] = '\0';
            ++i;
        } else {
            /* whatever need to be done if there is no room in the array */
        }
        /******* END - Code to capture key-value into appropriate array */
    }
}

/* Test iterating through the key-value pair constructed in query iteration */
fprintf(stdout, "--- Fields collected ---\n");
int keyIndex = 0;
for ( ; keyIndex < i; ++keyIndex) {
    fprintf(stdout, "{key: %s, value: %s}\n", temp_key[keyIndex], temp_value[keyIndex]);
}
Sign up to request clarification or add additional context in comments.

Comments

0
mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0);

  count_matched=0;    
  bson *doc;

  //Answer
  const char* temp_key[100][100],temp_value[100][100];
  int i=0;
  while(mongo_cursor_next(cursor)==MONGO_OK)
  {   
      count_matched++;

      doc=(bson *)mongo_cursor_bson(cursor);
      bson_iterator_init(&it,doc);

   while(bson_iterator_next(&it) != BSON_EOO)
   {
       fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it));
       temp[i][0]=bson_iterator_key[&it]; //Answer
       temp_value[i][0]=bson_iterator_key[&it]; //Answer
       i++;  //Answer
   }

  }

Just for the record, this is the rough sketch and i know about corruption of the temp variables and their overflow but i will remove it according to my code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.