1 /* count_delete.c */
2 
3 #include "test.h"
4 #include "mongo.h"
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 
main()9 int main() {
10     mongo conn[1];
11     bson b;
12     int i;
13 
14     const char *db = "test";
15     const char *col = "c.simple";
16     const char *ns = "test.c.simple";
17 
18     INIT_SOCKETS_FOR_WINDOWS;
19 
20     if ( mongo_connect( conn , TEST_SERVER , 27017 ) ) {
21         printf( "failed to connect\n" );
22         exit( 1 );
23     }
24 
25     /* if the collection doesn't exist dropping it will fail */
26     if ( !mongo_cmd_drop_collection( conn, "test", col, NULL )
27             && mongo_count( conn, db, col, NULL ) != 0 ) {
28         printf( "failed to drop collection\n" );
29         exit( 1 );
30     }
31 
32     for( i=0; i< 5; i++ ) {
33         bson_init( &b );
34 
35         bson_append_new_oid( &b, "_id" );
36         bson_append_int( &b , "a" , i+1 ); /* 1 to 5 */
37         bson_finish( &b );
38 
39         mongo_insert( conn , ns , &b );
40         bson_destroy( &b );
41     }
42 
43     /* query: {a: {$gt: 3}} */
44     bson_init( &b );
45     {
46         bson_append_start_object( &b, "a" );
47         bson_append_int( &b, "$gt", 3 );
48         bson_append_finish_object( &b );
49     }
50     bson_finish( &b );
51 
52     ASSERT( mongo_count( conn, db, col, NULL ) == 5 );
53     ASSERT( mongo_count( conn, db, col, &b ) == 2 );
54 
55     mongo_remove( conn, ns, &b );
56 
57     ASSERT( mongo_count( conn, db, col, NULL ) == 3 );
58     ASSERT( mongo_count( conn, db, col, &b ) == 0 );
59 
60     bson_destroy( &b );
61     mongo_cmd_drop_db( conn, db );
62     mongo_destroy( conn );
63     return 0;
64 }
65