MongoDB学习笔记【3】-- MongoDB C驱动使用

MongoDB官方手册:http://docs.mongodb.org/ecosystem/drivers/c/

下载mongodb C driver

[root@slayer ~]# git clone git://github.com/mongodb/mongo-c-driver.git

编译,安装

#cd mongo-c-driver
#make && make install

从官方手册找个demo mongo_c.c

#include <stdio.h>
#include <mongo.h>

int main()
{
   mongo conn[1];
   int status = mongo_client( conn, "127.0.0.1", 27017 );

   if( status != MONGO_OK ) {
      switch ( conn->err ) {
         case MONGO_CONN_NO_SOCKET:
             printf( "no socket\n" );
             return 1;
         case MONGO_CONN_FAIL:
             printf( "connection failed\n" );
             return 1;
         case MONGO_CONN_NOT_MASTER:
             printf( "not master\n" );
             return 1;
      }
   }
   printf( "connection done\n");

   mongo_destroy( conn );

   return 0;
}

编译,运行

[root@slayer mongo-c-driver]# gcc -lmongoc --std=c99 mongo_c.c -o mongo_c
[root@slayer mongo-c-driver]# ./mongo_c
./mongo_c: error while loading shared libraries: libmongoc.so.0.7: cannot open shar
ed object file: No such file or directory

找不到动态库,找找libmongoc.so.0.7在哪

[root@slayer mongo-c-driver]# find / -name libmongoc.so.0.7
/usr/local/lib/libmongoc.so.0.7

看看ld路径

[root@slayer mongo-c-driver]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
[root@slayer mongo-c-driver]# cat /etc/ld.so.conf.d/*
/usr/lib64/mysql
/usr/lib/xulrunner-2
/usr/lib64/xulrunner-2

果然没有/usr/local/lib,把它加上试试

[root@slayer mongo-c-driver]# echo /usr/local/lib/ > /etc/ld.so.conf.d/mongoc.conf
[root@slayer mongo-c-driver]# ldconfig
[root@slayer mongo-c-driver]# !gcc
gcc -lmongoc --std=c99 mongo_c.c -o mongo_c
[root@slayer mongo-c-driver]# ./mongo_c
connection done
[root@slayer mongo-c-driver]#

连接成功

简单插入和查询 mongo_c_wr.c

#include <stdlib.h>
#include <stdio.h>
#include <mongo.h>

static void tutorial_insert_batch( mongo *conn ) {
  bson *p, **ps;
  char *names[4];
  int ages[] = { 29, 24, 24, 32 };
  int i, n = 4;
  names[0] = "Eliot"; names[1] = "Mike"; names[2] = "Mathias"; names[3] = "Richard";

  ps = ( bson ** )malloc( sizeof( bson * ) * n);

  for ( i = 0; i < n; i++ ) {
    p = ( bson * )malloc( sizeof( bson ) );
    bson_init( p );
    bson_append_new_oid( p, "_id" );
    bson_append_string( p, "name", names[i] );
    bson_append_int( p, "age", ages[i] );
    bson_finish( p );
    ps[i] = p;
  }

  mongo_insert_batch( conn, "mydb.fc", ps, n, NULL, 0 );

  for ( i = 0; i < n; i++ ) {
    bson_destroy( ps[i] );
    free( ps[i] );
  }
}

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:
 * =====================================================================================
 */
   int
main ( int argc, char *argv[] )
{
   mongo conn[1];
   int status = mongo_client( conn, "127.0.0.1", 27017 );

   if( status != MONGO_OK ) {
      switch ( conn->err ) {
         case MONGO_CONN_NO_SOCKET:  printf( "no socket\n" ); return 1;
         case MONGO_CONN_FAIL:       printf( "connection failed\n" ); return 1;
         case MONGO_CONN_NOT_MASTER: printf( "not master\n" ); return 1;
      }
   }
   printf( "connection done\n" );


   /* make bson object */
   bson *b = (bson*) malloc(sizeof(bson));
   bson_init( b );
   bson_append_string( b, "name", "Joe" );
   bson_append_int( b, "age", 33);
   bson_finish( b );

   bson_print(b);

   /* insert bson object */
   mongo_insert( conn, "mydb.fc", b, NULL);

   /* destroy Joe */
   bson_destroy( b );
   free(b);

   /* batch insert */
   tutorial_insert_batch(conn);


   /* query */
   bson query[1];
   mongo_cursor  cursor[1];
   mongo_cursor_init( cursor, conn, "mydb.fc" );

   /* query from database */
   while (mongo_cursor_next(cursor) == MONGO_OK)
      bson_print(&cursor->current);

   mongo_cursor_destroy(cursor);

   mongo_destroy( conn );

   return EXIT_SUCCESS;

}           /* ----------  end of function main  ---------- */

编译运行

[root@slayer mongo-c-driver]# gcc --std=c99  -lmongoc -lbson mongo_c_wr.c -o mcwr
mongo_c_wr.c: In function 'tutorial_insert_batch':
mongo_c_wr.c:24: warning: passing argument 3 of 'mongo_insert_batch' from incompatible pointer type
[root@postgresql mongo-c-driver]# ./mcwr
connection done
        name : 2         Joe
        age : 16         33
        _id : 7          50c99a05c0ac94782d63d5a1
        name : 2         Joe
        age : 16         33
        _id : 7          50c99a05c8730a6b00000000
        name : 2         Eliot
        age : 16         29
        _id : 7          50c99a05c8730a6b00000001
        name : 2         Mike
        age : 16         24
        _id : 7          50c99a05c8730a6b00000002
        name : 2         Mathias
        age : 16         24
        _id : 7          50c99a05c8730a6b00000003
        name : 2         Richard
        age : 16         32

 

 

MongoDB学习笔记【1】-- 安装启动

MongoDB学习笔记【2】-- 试用

MongoDB学习笔记【3】-- MongoDB C驱动使用

MongoDB学习笔记【4】-- MongoDB Java驱动使用

posted @ 2013-03-01 14:43  Leo Forest  阅读(1224)  评论(0编辑  收藏  举报