0
点赞
收藏
分享

微信扫一扫

c操作mongodb增删改查

半夜放水 2022-04-14 阅读 64
c++

1 inster:

#include <bson.h>

#include <mongoc.h>

#include <stdio.h>

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

{undefined

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)) {undefined

    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[])

{undefined

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)) {undefined

   if (!mongoc_collection_remove (

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

   {undefined

      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[])

{undefined

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)) {undefined

  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)) {undefined

  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[])

{undefined

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)) {undefined

  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;

}

举报

相关推荐

0 条评论