Created
September 25, 2015 00:17
-
-
Save sindbach/0911be9e892d87aed5a1 to your computer and use it in GitHub Desktop.
A simple C example to insert a sub document in MongoDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bson.h> | |
#include <mongoc.h> | |
#include <stdio.h> | |
int main (int argc, char *argv[]) | |
{ | |
mongoc_client_t *client; | |
mongoc_collection_t *collection; | |
mongoc_cursor_t *cursor; | |
bson_error_t error; | |
bson_oid_t oid; | |
bson_t *doc; | |
mongoc_init (); | |
client = mongoc_client_new ("mongodb://localhost:27017/"); | |
collection = mongoc_client_get_collection (client, "test", "test"); | |
doc = bson_new (); | |
bson_t child; | |
BSON_APPEND_DOCUMENT_BEGIN(doc, "students", &child); | |
BSON_APPEND_UTF8(&child, "name", "Oconnor"); | |
BSON_APPEND_UTF8(&child, "status", "true"); | |
BSON_APPEND_INT32(&child, "employee", 100); | |
bson_append_document_end(doc, &child); | |
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) { | |
printf ("%s\n", error.message); | |
} | |
bson_destroy (doc); | |
mongoc_collection_destroy (collection); | |
mongoc_client_destroy (client); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment