您的位置:首页 > 数据库 > Mongodb

MongoDB c 增删改查

2017-11-20 14:48 127 查看
1 inster:

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

 

    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;

}

 

2 del

#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;

   bson_t *query;

   char *str;

 

   mongoc_init ();

 

   client = mongoc_client_new (

      "mongodb://localhost:27017/?appname=find-specific-example");

   collection = mongoc_client_get_collection (client, "mydb", "mycoll");

   query = bson_new ();

   BSON_APPEND_UTF8 (query, "hello", "world");

 

   cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL);

 

   while (mongoc_cursor_next (cursor, &doc)) {

       if (!mongoc_collection_remove (

          collection, MONGOC_REMOVE_SINGLE_REMOVE, doc, NULL, &error))

       {

          fprintf (stderr, "Delete failed: %s\n", error.message);

          printf ("Delete failed: %s\n", error.message);

       }

      str = bson_as_json (doc, NULL);

      printf ("%s\n", str);

      bson_free (str);

   }

 

   bson_destroy (doc);

   mongoc_collection_destroy (collection);

   mongoc_cursor_destroy (cursor);

   mongoc_client_destroy (client);

   mongoc_cleanup ();

 

   return 0;

}

 

3 update;

#include <bcon.h>

#include <bson.h>

#include <mongoc.h>

#include <stdio.h>

 

int main (int argc, char *argv[])

{

   mongoc_collection_t *collection;

   mongoc_client_t *client;

   bson_error_t error;

   bson_oid_t oid;

   bson_t *doc = NULL;

   bson_t *update = NULL;

   bson_t *query = NULL;

 

   mongoc_init ();

 

   client =

      mongoc_client_new ("mongodb://localhost:27017/?appname=update-example");

   collection = mongoc_client_get_collection (client, "mydb", "mycoll");

 

   bson_oid_init (&oid, NULL);

   doc = BCON_NEW ("_id", BCON_OID (&oid), "key", BCON_UTF8 ("old_value"));

 

   if (!mongoc_collection_insert (

          collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {

      fprintf (stderr, "%s\n", error.message);

      goto fail;

   }

 

   query = BCON_NEW ("_id", BCON_OID (&oid));

   update = BCON_NEW ("$set",

                      "{",

                      "key",

                      BCON_UTF8 ("new_value"),

                      "updated",

                      BCON_BOOL (true),

                      "}");

 

   if (!mongoc_collection_update (

          collection, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {

      fprintf (stderr, "%s\n", error.message);

      goto fail;

   }

 

fail:

   if (doc)

      bson_destroy (doc);

   if (query)

      bson_destroy (query);

   if (update)

      bson_destroy (update);

 

   mongoc_collection_destroy (collection);

   mongoc_client_destroy (client);

   mongoc_cleanup ();

 

   return 0;

}

 

4 Find

#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;

   const bson_t *doc;

   bson_t *query;

   char *str;

 

   mongoc_init ();

 

   client =

      mongoc_client_new ("mongodb://localhost:27017/?appname=find-example");

   collection = mongoc_client_get_collection (client, "mydb", "mycoll");

   query = bson_new ();

   cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL);

 

   while (mongoc_cursor_next (cursor, &doc)) {

      str = bson_as_json (doc, NULL);

      printf ("%s\n", str);

      bson_free (str);

   }

 

   bson_destroy (query);

   mongoc_cursor_destroy (cursor);

   mongoc_collection_destroy (collection);

   mongoc_client_destroy (client);

   mongoc_cleanup ();

 

   return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  云计算 数据库 c