I have a very simple question in appearance but for which I've been stuck for several days.

What method would you consider (the most effective one, if you know several) to retrieve all the data present in a collection ?

Here's the structure of my collection example :

collection = {
   _id: ObjectId(""),
   field: ""
}

Best regards.

有帮助吗?

解决方案

To construct a query in mongodb-c driver,

  bson_init( query );
  bson_finish( query );

// if you want to add more search parameters then in between those two add

  bson_append_int( query, "age", 24 );

You can keep adding more search criteria.

Here is one example:

  bson query[1];
  mongo_cursor cursor[1];

  bson_init( query );
  bson_append_int( query_buf, "age", 24 ); // to match particular criteria, remove this line to match call documents
  bson_finish( query );

  mongo_cursor_init( cursor, conn, "test.collection" );
  mongo_cursor_set_query( cursor, query )

  while( mongo_cursor_next( cursor ) == MONGO_OK ) {
    bson_iterator iterator[1];
    if ( bson_find( iterator, mongo_cursor_bson( cursor ), "name" )) {
        printf( "name: %s\n", bson_iterator_string( it ) );
    }
  }

  bson_destroy( query );
  mongo_cursor_destroy( cursor );

This is equivalent to:

db.collection.find()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top