MongoDB简单使用(C语言)

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc, char *argv[])
{
	mongoc_client_t *client;
	mongoc_collection_t *collection;
	bson_error_t error;
	bson_oid_t oid;
	bson_t *doc;

	mongoc_init ();

	client = mongoc_client_new ("mongodb://localhost:27017/?appname=insert-example");
	collection = mongoc_client_get_collection (client, "mydb", "mycoll");

	doc = bson_new ();
	bson_oid_init (&oid, NULL);
	BSON_APPEND_OID (doc, "_id", &oid);
	BSON_APPEND_UTF8 (doc, "hello", "world");
	BSON_APPEND_UTF8 (doc, "name", "yqq");
	BSON_APPEND_UTF8 (doc, "age", "23");
	BSON_APPEND_UTF8 (doc, "addr", "shenzhen");

	if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
		fprintf (stderr, "%s\n", error.message);
	}

	bson_destroy (doc);
	mongoc_collection_destroy (collection);
	mongoc_client_destroy (client);
	mongoc_cleanup ();

	return 0;
}

Makefile

test: test.c
	gcc test.c -o test -I/usr/local/include/libmongoc-1.0/ -I/usr/local/include/libbson-1.0/ -lmongoc-1.0 -lbson-1.0
clean:
	rm test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值