top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

MongoDB : How mongoDB works for C programming language ?

+4 votes
452 views
MongoDB : How mongoDB works for C programming language ?
posted Jul 9, 2015 by Harshita

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Here is the tutorial link where you will see how c programming works with MongoDB with an example
http://api.mongodb.org/c/current/tutorial.html
This link contains all required tools that we need to program with mongodb and C. it will explain how to do CRUD operations.
Like for inserting a document in DB the code is:

#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/");
    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");

    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;
}
answer Feb 26, 2016 by Shivam Kumar Pandey
Similar Questions
+1 vote

Query description: I have a collection name student_db and added a document in a collection with entries (name: "alok, age : 31 and profession: "xyz"). After inserting the document into student_db database, I want to add another (key:value ) pair i.e. salary: 50000.

Can someone help me out to resolve this problem ?

...