mongodb gridfs 使用driver c 按照linux时间戳进行删除文件

/*   gcc -o test test.c -I/usr/local/include/libbson-1.0 \
-I/usr/local/include/libmongoc-1.0 -lmongoc-1.0 -lbson-1.0                    */

#include 
#include 
#include 

#define DATABASE "ai"

int main(int argc, char *argv[])
{
    const char *uri_string = "mongodb://localhost:27017/?appname=new-gridfs-example";
    mongoc_client_t *client;   //客户端句柄
    mongoc_database_t *db;      //库句柄
    mongoc_gridfs_bucket_t *bucket;   //gridfs句柄
    mongoc_cursor_t *cursor;     //游标卡尺
	bson_t query;   //查询条件
    bson_t* opts =NULL;  
	int timestamp;   //时间戳
    bool res;   //返回值
    bson_error_t error;  //返回的错误码
    const bson_t *doc;   //存储游标卡尺的数据结果
    char *str;   //保存转换的json数据

	bson_t *command;   //执行命令的bson
	bson_t reply;     //执行命令的结果

    mongoc_init();

    /* 1. Make a bucket. */
    client = mongoc_client_new(uri_string);
    db     = mongoc_client_get_database(client, DATABASE);
    bucket = mongoc_gridfs_bucket_new(db, NULL, NULL, &error);
    if (!bucket)
    {
        printf("Error creating gridfs bucket: %s\n", error.message);
        return EXIT_FAILURE;
    }

//比较查询语句
#if 1   
	//({"metadata.time":{$lt:1590715334}}).sort({"metadata.time":1})

	bson_t child2, child3;
	bson_init(&query);
	bson_append_document_begin(&query, "metadata.time", -1, &child2);
	timestamp = 1590646522;
	BSON_APPEND_INT64(&child2, "$gte", timestamp);
	bson_append_document_end(&query, &child2);
	str = bson_as_json(&query, NULL);
	printf("\n%s\n", str);
#endif

	//sort表示排序,BCON_INT32(-1)表示反向排线  BCON_INT32(1)表示正向排序
    opts = BCON_NEW("sort", "{", "metadate.time", BCON_INT32(-1), "}");
    cursor = mongoc_gridfs_bucket_find(bucket, &query, opts);
    while (mongoc_cursor_next(cursor, &doc))
    {		
		printf("输出bson_t信息!\r\n");
//这个是删除的代码
#if 1      
		bson_iter_t iter;
		if (bson_iter_init(&iter, doc) && bson_iter_find(&iter, "_id"))
		{
			printf("------------------>find bson_value_t\n");

			//value 只读的  不能被修改和释放
			const bson_value_t * value = bson_iter_value(&iter);

			printf("iter value_type=%d\r\n", value->value_type);

			if (value->value_type == BSON_TYPE_OID)
			{
				res = mongoc_gridfs_bucket_delete_by_id(bucket, value, &error);
				if (!res)
				{
					printf("Error deleting the file: %s\n", error.message);
					return EXIT_FAILURE;
				}
				printf("====delete sucssed === \r\n");

			}
		}
#endif		
        str = bson_as_canonical_extended_json(doc, NULL);
        printf("%s\n", str);
        bson_free(str);
    }

//执行命令
#if 1
	//执行情况内存的命令
	command = BCON_NEW(
		"repairDatabase",
		BCON_INT32(1));
	str = bson_as_json(command, NULL);
	printf("\n%s\n", str);
	if (mongoc_client_command_simple(
		client, DATABASE,command, NULL, &reply, &error)) {
		str = bson_as_canonical_extended_json(&reply, NULL);
		printf("%s\n", str);
		bson_free(str);
	}
	else {
		fprintf(stderr, "Failed to run command: %s\n", error.message);
	}	
#endif

	bson_destroy(command);
	bson_destroy(&reply);
    bson_destroy(opts);
    mongoc_cursor_destroy(cursor);
    mongoc_gridfs_bucket_destroy(bucket);
    mongoc_database_destroy(db);
    mongoc_client_destroy(client);
    mongoc_cleanup();

    return EXIT_SUCCESS;
}

 

你可能感兴趣的:(mongodb)