static void
test_insert (void)
{
   mongoc_database_t *database;
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_context_t *context;
   bson_error_t error;
   bool r;
   bson_oid_t oid;
   unsigned i;
   bson_t b;


   client = mongoc_client_new(gTestUri);
   ASSERT (client);

   database = get_test_database (client);
   ASSERT (database);

   collection = get_test_collection (client, "test_insert");
   ASSERT (collection);

   mongoc_collection_drop(collection, &error);

   context = bson_context_new(BSON_CONTEXT_NONE);
   ASSERT (context);

   for (i = 0; i < 10; i++) {
      bson_init(&b);
      bson_oid_init(&oid, context);
      bson_append_oid(&b, "_id", 3, &oid);
      bson_append_utf8(&b, "hello", 5, "/world", 5);
      r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL,
                                   &error);
      if (!r) {
         MONGOC_WARNING("%s\n", error.message);
      }
      ASSERT (r);
      bson_destroy(&b);
   }

   bson_init (&b);
   BSON_APPEND_INT32 (&b, "$hello", 1);
   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &b, NULL,
                                 &error);
   ASSERT (!r);
   ASSERT (error.domain == MONGOC_ERROR_BSON);
   ASSERT (error.code == MONGOC_ERROR_BSON_INVALID);
   bson_destroy (&b);

   r = mongoc_collection_drop (collection, &error);
   ASSERT (r);

   mongoc_collection_destroy(collection);
   mongoc_database_destroy(database);
   bson_context_destroy(context);
   mongoc_client_destroy(client);
}
int
main (int   argc,
      char *argv[])
{
   mongoc_client_t *client;
   mongoc_apm_callbacks_t *callbacks;
   stats_t stats = { 0 }; 
   mongoc_collection_t *collection;
   const char *uristr = "mongodb://127.0.0.1/?appname=cmd-monitoring-example";
   const char *collection_name = "test";
   bson_t doc;

   mongoc_init ();

   if (argc > 1) {
      uristr = argv [1];
   }

   client = mongoc_client_new (uristr);

   if (!client) {
      fprintf (stderr, "Failed to parse URI.\n");
      return EXIT_FAILURE;
   }

   mongoc_client_set_error_api (client, 2);
   callbacks = mongoc_apm_callbacks_new ();
   mongoc_apm_set_command_started_cb (callbacks, command_started);
   mongoc_apm_set_command_succeeded_cb (callbacks, command_succeeded );
   mongoc_apm_set_command_failed_cb (callbacks, command_failed);
   mongoc_client_set_apm_callbacks (client,
                                    callbacks,
                                    (void *) &stats /* context pointer */);

   bson_init (&doc);
   BSON_APPEND_INT32 (&doc, "_id", 1);

   collection = mongoc_client_get_collection (client, "test", collection_name);
   mongoc_collection_drop (collection, NULL);
   mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, NULL);
   /* duplicate key error on the second insert */
   mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, NULL);

   printf ("started: %d\nsucceeded: %d\nfailed: %d\n",
           stats.started, stats.succeeded, stats.failed);

   bson_destroy (&doc);
   mongoc_collection_destroy (collection);
   mongoc_apm_callbacks_destroy (callbacks);
   mongoc_client_destroy (client);

   mongoc_cleanup ();

   return EXIT_SUCCESS;
}
Exemplo n.º 3
0
mrb_value mrb_mongo_collection_insert(mrb_state *mrb, mrb_value self) {
  mrb_mongo_collection_data *data = DATA_PTR(self);
  mrb_value record_hash, inserted_hash;
  bson_t *doc;
  bson_oid_t oid;
  bson_error_t error;

  mrb_get_args(mrb, "H", &record_hash);

  doc = bson_new();
  mrb_hash_to_bson(mrb, record_hash, doc);

  //add id if not supplied
  if (!bson_has_field(doc, "_id")) {
    bson_oid_init(&oid, NULL);
    bson_append_oid(doc, "_id", -1, &oid);
  }

  if (!mongoc_collection_insert(data->collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
    bson_destroy(doc);
    mrb_raise(mrb, E_RUNTIME_ERROR, error.message);
  }

  inserted_hash = mrb_hash_new(mrb);
  bson_to_mrb_hash(mrb, doc, inserted_hash);
  bson_destroy(doc);

  return inserted_hash;
}
static void
test_rename (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_error_t error;
   bson_t doc = BSON_INITIALIZER;
   bool r;

   client = mongoc_client_new (gTestUri);
   ASSERT (client);

   collection = get_test_collection (client, "test_rename");
   ASSERT (collection);

   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error);
   assert (r);

   r = mongoc_collection_rename (collection, "test", "test_rename_2", false, &error);
   assert (r);

   r = mongoc_collection_drop (collection, &error);
   assert (r);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   bson_destroy (&doc);
}
Exemplo n.º 5
0
static void saveAlarmLog(mongoc_collection_t *coll,char* se_id,int code,char * tel,char * msg,char* userId,char * device_name,char* name,char * de_id){
     bson_t *sdoc = bson_new ();
     bson_oid_t a_oid;
     bson_oid_init (&a_oid, NULL);
     BSON_APPEND_OID (sdoc, "_id", &a_oid);
     //BSON_APPEND_OID (sdoc, "device_id", de_oid);
     //BSON_APPEND_UTF8 (sdoc, "name", name);
     //BSON_APPEND_INT32 (sdoc, "sensorType", sensorType);
     BSON_APPEND_INT32 (sdoc, "code", code);
     BSON_APPEND_UTF8 (sdoc, "mobile", tel);
     BSON_APPEND_UTF8 (sdoc, "sendmsg", msg);
     BSON_APPEND_UTF8 (sdoc, "user_id", userId);
     BSON_APPEND_UTF8 (sdoc, "se_id", se_id);
     BSON_APPEND_UTF8 (sdoc, "de_id", de_id);
     BSON_APPEND_UTF8 (sdoc, "device_name", device_name);
     BSON_APPEND_UTF8 (sdoc, "sensor_name", name);
     time_t timep;
     time(&timep);
     BSON_APPEND_DOUBLE (sdoc, "time", timep);
     bson_error_t serror;
     if (!mongoc_collection_insert (coll, MONGOC_INSERT_NONE, sdoc, NULL, &serror)) {
        fprintf (stderr, "%s\n", serror.message);
     }
     bson_destroy (sdoc);
}
static void
test_drop (void)
{
   mongoc_collection_t *collection;
   mongoc_database_t *database;
   mongoc_client_t *client;
   bson_error_t error;
   bson_t *doc;
   bool r;

   client = mongoc_client_new(gTestUri);
   ASSERT (client);

   database = get_test_database (client);
   ASSERT (database);

   collection = get_test_collection (client, "test_drop");
   ASSERT (collection);

   doc = BCON_NEW("hello", "world");
   r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error);
   bson_destroy (doc);
   ASSERT (r);

   r = mongoc_collection_drop(collection, &error);
   ASSERT (r == true);

   r = mongoc_collection_drop(collection, &error);
   ASSERT (r == false);

   mongoc_collection_destroy(collection);
   mongoc_database_destroy(database);
   mongoc_client_destroy(client);
}
Exemplo n.º 7
0
static void
test1 (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_error_t error = { 0 };
   bool r;
   bson_t q = BSON_INITIALIZER;
   int i;

   BSON_ASSERT (cluster);

   bson_append_utf8 (&q, "hello", -1, "world", -1);

   client = ha_sharded_cluster_get_client (cluster);
   collection = mongoc_client_get_collection (client, "test", "test");

   for (i = 0; i < 100; i++) {
      r = mongoc_collection_insert (collection,
                                    MONGOC_INSERT_NONE,
                                    &q,
                                    NULL,
                                    &error);
      BSON_ASSERT (r);
      BSON_ASSERT (!error.domain);
      BSON_ASSERT (!error.code);
   }

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   bson_destroy (&q);
}
Exemplo n.º 8
0
void Collection::CollectionImpl::Insert( tlib::bson::object doc )
{
	bson_error_t error;

	bool res = mongoc_collection_insert( m_collection.get(), MONGOC_INSERT_NONE, doc.get(), NULL, &error );
	if(!res)
		throw MongoError( std::string(error.message).append(" in Collection::Insert") );
}
static void
test_large_return (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   mongoc_cursor_t *cursor;
   bson_error_t error;
   const bson_t *doc = NULL;
   bson_oid_t oid;
   bson_t insert_doc = BSON_INITIALIZER;
   bson_t query = BSON_INITIALIZER;
   size_t len;
   char *str;
   bool r;

   client = mongoc_client_new (gTestUri);
   ASSERT (client);

   collection = get_test_collection (client, "test_large_return");
   ASSERT (collection);

   len = 1024 * 1024 * 4;
   str = bson_malloc (len);
   memset (str, (int)' ', len);
   str [len - 1] = '\0';

   bson_oid_init (&oid, NULL);
   BSON_APPEND_OID (&insert_doc, "_id", &oid);
   BSON_APPEND_UTF8 (&insert_doc, "big", str);

   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &insert_doc, NULL, &error);
   assert (r);

   bson_destroy (&insert_doc);

   BSON_APPEND_OID (&query, "_id", &oid);

   cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL);
   assert (cursor);
   bson_destroy (&query);

   r = mongoc_cursor_next (cursor, &doc);
   assert (r);
   assert (doc);

   r = mongoc_cursor_next (cursor, &doc);
   assert (!r);

   mongoc_cursor_destroy (cursor);

   r = mongoc_collection_drop (collection, &error);
   assert (r);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   bson_free (str);
}
static void
test_delete (void)
{
   mongoc_collection_t *collection;
   mongoc_database_t *database;
   mongoc_client_t *client;
   bson_context_t *context;
   bson_error_t error;
   bool r;
   bson_oid_t oid;
   bson_t b;
   int i;

   client = mongoc_client_new(gTestUri);
   ASSERT (client);

   database = get_test_database (client);
   ASSERT (database);

   collection = get_test_collection (client, "test_delete");
   ASSERT (collection);

   context = bson_context_new(BSON_CONTEXT_NONE);
   ASSERT (context);

   for (i = 0; i < 100; i++) {
      bson_init(&b);
      bson_oid_init(&oid, context);
      bson_append_oid(&b, "_id", 3, &oid);
      bson_append_utf8(&b, "hello", 5, "world", 5);
      r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL,
                                   &error);
      if (!r) {
         MONGOC_WARNING("%s\n", error.message);
      }
      ASSERT (r);
      bson_destroy(&b);

      bson_init(&b);
      bson_append_oid(&b, "_id", 3, &oid);
      r = mongoc_collection_delete(collection, MONGOC_DELETE_NONE, &b, NULL,
                                   &error);
      if (!r) {
         MONGOC_WARNING("%s\n", error.message);
      }
      ASSERT (r);
      bson_destroy(&b);
   }

   r = mongoc_collection_drop (collection, &error);
   ASSERT (r);

   mongoc_collection_destroy(collection);
   mongoc_database_destroy(database);
   bson_context_destroy(context);
   mongoc_client_destroy(client);
}
Exemplo n.º 11
0
SEXP R_mongo_collection_insert_bson(SEXP ptr_col, SEXP ptr_bson, SEXP stop_on_error){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *b = r2bson(ptr_bson);
  mongoc_insert_flags_t flags = Rf_asLogical(stop_on_error) ? MONGOC_INSERT_NONE : MONGOC_INSERT_CONTINUE_ON_ERROR;
  bson_error_t err;

  if(!mongoc_collection_insert(col, flags, b, NULL, &err))
    stop(err.message);

  return Rf_ScalarLogical(1);
}
Exemplo n.º 12
0
int telemetry_mongo_insert(centernode_t *cn, cnaccess_t* ca, cdnmsg_t *req)
{
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_t *doc;
    char docbuf[256] = {0};

#pragma pack(1)
    struct Info_s {
        uint16_t addr;
        uint32_t status;
    } *pInfo;
#pragma pack(4)
    int  size = (ntohl(req->pdu.len)-23)/6;
    uint8_t mytime[7];
    memcpy ((void*)mytime, ((char*)&req->pdu + ntohl(req->pdu.len)-7), 7);
    pInfo = (struct Info_s*)&req->pdu.body.telemetry.addr;


    int i = 0;
    for (i=0; i<size; i++ ) {
        sprintf (docbuf, "{\"type\":%d,\"nodeid\":%d,\"addr\":%d,\"data\":%d,\"time\":\"20%02d-%02d-%02d %02d:%02d:%02d.%03d\"}",
                ntohs(req->pdu.cmd), ntohs(req->pdu.body.login.nodeid),
                ntohs(pInfo->addr),
                ntohl(pInfo->status),
                mytime[0],
                mytime[1],
                mytime[2],
                mytime[3],
                mytime[4],
                mytime[5],
                mytime[6]);

        doc = bson_new_from_json((uint8_t*)docbuf, strlen(docbuf), &error);
        if (strcmp (error.message, "") != 0) {
            fprintf (stderr, "%s\n", error.message);
            return 0;
        }
        //        collection = getValidColl ();
        char szaddr[128] = {0};
        sprintf (szaddr, "%x_%x_%x", ntohs(req->pdu.body.login.nodeid),ntohs(pInfo->addr),ntohs(req->pdu.cmd));
        pthread_mutex_lock (&cn->Mongo[0].MongoLock);
        //        collection = cn->Mongo[0].MongoCollection;
        collection = mongoc_client_get_collection (cn->Mongo[0].MongoClient, "mydb", szaddr);
        if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
            fprintf (stderr, "%s\n", error.message);
        }   
        mongoc_collection_destroy (collection);
        pthread_mutex_unlock (&cn->Mongo[0].MongoLock);
        bson_destroy (doc);
        pInfo++;
    }
    return 0;
}
static void
test_regex (void)
{
   mongoc_collection_t *collection;
   mongoc_database_t *database;
   mongoc_write_concern_t *wr;
   mongoc_client_t *client;
   bson_error_t error = { 0 };
   int64_t count;
   bson_t q = BSON_INITIALIZER;
   bson_t *doc;
   bool r;

   client = mongoc_client_new (gTestUri);
   ASSERT (client);

   database = get_test_database (client);
   ASSERT (database);

   collection = get_test_collection (client, "test_regex");
   ASSERT (collection);

   wr = mongoc_write_concern_new ();
   mongoc_write_concern_set_journal (wr, true);

   doc = BCON_NEW ("hello", "/world");
   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, wr, &error);
   ASSERT (r);

   BSON_APPEND_REGEX (&q, "hello", "^/wo", "i");

   count = mongoc_collection_count (collection,
                                    MONGOC_QUERY_NONE,
                                    &q,
                                    0,
                                    0,
                                    NULL,
                                    &error);

   ASSERT (count > 0);
   ASSERT (!error.domain);

   r = mongoc_collection_drop (collection, &error);
   ASSERT (r);

   mongoc_write_concern_destroy (wr);

   bson_destroy (&q);
   bson_destroy (doc);
   mongoc_collection_destroy (collection);
   mongoc_database_destroy(database);
   mongoc_client_destroy (client);
}
Exemplo n.º 14
0
int mongo_add_document(mongoc_client_t *client, char *database, char *collection_name, bson_t *document) {
    mongoc_collection_t *collection;
    bson_error_t error;

    collection = mongoc_client_get_collection(client, database, collection_name);

    if (!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, document, NULL, &error)) {
        printf("%s\n", error.message);
        return -1;
    }
    mongoc_collection_destroy(collection);
    return 0;
}
Exemplo n.º 15
0
static void
test_mongoc_client_authenticate_failure (void)
{
    mongoc_collection_t *collection;
    mongoc_cursor_t *cursor;
    mongoc_client_t *client;
    const bson_t *doc;
    bson_error_t error;
    bool r;
    bson_t q;
    bson_t empty = BSON_INITIALIZER;

    /*
     * Try authenticating with that user.
     */
    bson_init(&q);
    client = mongoc_client_new(gTestUriWithBadPassword);
    collection = mongoc_client_get_collection(client, "test", "test");
    cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0,
                                    &q, NULL, NULL);
    r = mongoc_cursor_next(cursor, &doc);
    assert(!r);
    r = mongoc_cursor_error(cursor, &error);
    assert(r);
    assert(error.domain == MONGOC_ERROR_CLIENT);
    assert(error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE);
    mongoc_cursor_destroy(cursor);

    /*
     * Try various commands while in the failed state to ensure we get the
     * same sort of errors.
     */
    r = mongoc_collection_insert (collection, 0, &empty, NULL, &error);
    assert (!r);
    assert (error.domain == MONGOC_ERROR_CLIENT);
    assert (error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE);

    /*
     * Try various commands while in the failed state to ensure we get the
     * same sort of errors.
     */
    r = mongoc_collection_update (collection, 0, &q, &empty, NULL, &error);
    assert (!r);
    assert (error.domain == MONGOC_ERROR_CLIENT);
    assert (error.code == MONGOC_ERROR_CLIENT_AUTHENTICATE);

    mongoc_collection_destroy(collection);
    mongoc_client_destroy(client);
}
Exemplo n.º 16
0
/*
 * Insert a document 'b' into MongoDB.
 */
bool
MongoInsert(MONGO_CONN* conn, char *database, char* collection, BSON* b)
{
	mongoc_collection_t *c = NULL;
	bson_error_t error;
	bool r = false;

	c = mongoc_client_get_collection(conn, database, collection);

	r = mongoc_collection_insert(c, MONGOC_INSERT_NONE, b, NULL, &error);
	mongoc_collection_destroy(c);
	if (!r)
		ereport(ERROR, (errmsg("failed to insert row"),
						errhint("Mongo error: \"%s\"", error.message)));
	return true;
}
static void
test_validate (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_iter_t iter;
   bson_error_t error;
   bson_t doc = BSON_INITIALIZER;
   bson_t opts = BSON_INITIALIZER;
   bson_t reply;
   bool r;

   client = mongoc_client_new (gTestUri);
   ASSERT (client);

   collection = get_test_collection (client, "test_validate");
   ASSERT (collection);

   r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &doc, NULL, &error);
   assert (r);

   BSON_APPEND_BOOL (&opts, "full", true);

   r = mongoc_collection_validate (collection, &opts, &reply, &error);
   assert (r);

   assert (bson_iter_init_find (&iter, &reply, "ns"));
   assert (bson_iter_init_find (&iter, &reply, "valid"));

   bson_destroy (&reply);

   bson_reinit (&opts);
   BSON_APPEND_UTF8 (&opts, "full", "bad_value");

   r = mongoc_collection_validate (collection, &opts, &reply, &error);
   assert (!r);
   assert (error.domain == MONGOC_ERROR_BSON);
   assert (error.code == MONGOC_ERROR_BSON_INVALID);

   r = mongoc_collection_drop (collection, &error);
   assert (r);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   bson_destroy (&doc);
   bson_destroy (&opts);
}
Exemplo n.º 18
0
static void dump_message(ullong number, str_t type, msg_content_array_t* content)
{
    bson_t document;
    bson_error_t error;
    time_t t;
    mongoc_collection_t* collection = mongoc_database_get_collection(robot.mongoc_database, "message");
    char* json = msg_content_array_to_json_object_string(content, "content");

    time(&t);
    if (!bson_init_from_json(&document, json, strlen(json), &error)) MONGOC_WARNING("%s\n", error.message);
    BSON_APPEND_INT64(&document, "from", number);
    BSON_APPEND_UTF8(&document, "type", type.ptr);
    BSON_APPEND_TIME_T(&document, "time", t);
    if (!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &document, NULL, &error)) MONGOC_WARNING("%s\n", error.message);
    bson_destroy(&document);
    free(json);
}
Exemplo n.º 19
0
void PruebaMongoDB()
{
	mongoc_collection_t *collection;
	   mongoc_client_t *client;
	   mongoc_cursor_t *cursor;
	   const bson_t *item;
	   bson_error_t error;
	   bson_oid_t oid;
	   bson_t *query;
	   bson_t *doc;
	   char *str;
	   bool r;

	   mongoc_init();

	   /* get a handle to our collection */
	   client = mongoc_client_new ("mongodb://localhost:27017");
	   collection = mongoc_client_get_collection (client, "local", "tito");

	   /* insert a document */
	    bson_oid_init (&oid, NULL);
	    doc = BCON_NEW ("_id", BCON_OID (&oid),
	                    "hello", BCON_UTF8 ("world!"));
	    r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error);
	    if (!r) {
	       fprintf (stderr, "%s\n", error.message);
	    }

	    /* build a query to execute */
	    query = BCON_NEW ("_id", BCON_OID (&oid));

	    /* execute the query and iterate the results */
	    cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
	    while (mongoc_cursor_next (cursor, &item)) {
	       str = bson_as_json (item, NULL);
	       printf ("%s\n", str);
	       bson_free (str);
	    }

	    /* release everything */
	    mongoc_cursor_destroy (cursor);
	    mongoc_collection_destroy (collection);
	    mongoc_client_destroy (client);
	    bson_destroy (query);
	    bson_destroy (doc);
}
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;

    /* Init mongo C driver */
    mongoc_init ();

    /* Create a new connection to the database a get the specified collection  */
    client = mongoc_client_new ("mongodb://localhost:27017/");
    if (!client) {
        fprintf(stderr, "Connection to database failed!");
        mongoc_cleanup();
        return 0;
    } else {
        printf("Connected to the database successfully\n");
    }
    collection = mongoc_client_get_collection (client, "testdb", "users");

    /* Create a new document (All the documents must have the _id field to be used as primary key) */
    doc = bson_new ();
    bson_oid_init (&oid, NULL);
    BSON_APPEND_OID (doc, "_id", &oid);
    BSON_APPEND_UTF8 (doc, "username", "John");
    BSON_APPEND_UTF8 (doc, "password", "123456");

    /* Insert the document into the collection */
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        printf ("Error inserting the document. - %s\n", error.message);
    } else {
        printf ("Document successfully inserted!\n");
    }

    /* Clean the doc, colletion and client allocations */
    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    mongoc_client_destroy (client);

    /* Cleanup after mongo C driver */
    mongoc_cleanup();

    return 0;
}
Exemplo n.º 21
0
    static Variant HHVM_METHOD(MongoCollection, insert, Variant a, Array options) {
        mongoc_collection_t *collection;
        bson_t doc;
        bson_error_t error;

        collection = get_collection(this_);

        Array& doc_array = a.toArrRef();
        if (!doc_array.exists(String("_id"))) {
            const StaticString s_MongoId("MongoId");
            char id[25];
            bson_oid_t oid;
            bson_oid_init(&oid, NULL);
            bson_oid_to_string(&oid, id);
            ObjectData * data = create_object(&s_MongoId, make_packed_array(String(id)));
            doc_array.add(String("_id"), data);
        }
        encodeToBSON(doc_array, &doc);




        int w_flag = MONGOC_WRITE_CONCERN_W_DEFAULT;
        //如果传递了参数
        
        mongoc_write_concern_t *write_concern;
        write_concern = mongoc_write_concern_new();
        mongoc_write_concern_set_w(write_concern, w_flag);
        
        bool ret = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &doc, write_concern, &error);
        if (!ret) {
            mongoThrow<MongoCursorException>((const char *) error.message);
        }
        mongoc_collection_destroy(collection);
        bson_destroy(&doc);
        return ret;
        /*
        bool mongoc_collection_insert (mongoc_collection_t           *collection,
                                      mongoc_insert_flags_t          flags,
                                      const bson_t                  *document,
                                      const mongoc_write_concern_t  *write_concern,
                                      bson_error_t                  *error);
         */

    }
static void
test_replica_set_ssl_client(void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   ha_replica_set_t *replica_set;
   bson_error_t error;
   int r;
   bson_t b;

   mongoc_ssl_opt_t sopt = { 0 };

   sopt.pem_file = gTestPEMFileLocalhost;
   sopt.ca_file = gTestCAFile;

   replica_set = ha_replica_set_new("repltest1");
   ha_replica_set_ssl(replica_set, &sopt);
   ha_replica_set_add_replica(replica_set, "replica1");
   ha_replica_set_add_replica(replica_set, "replica2");
   ha_replica_set_add_replica(replica_set, "replica3");

   ha_replica_set_start(replica_set);
   ha_replica_set_wait_for_healthy(replica_set);


   client = ha_replica_set_create_client(replica_set);
   assert(client);

   collection = mongoc_client_get_collection(client, "test", "test");
   assert(collection);

   bson_init(&b);
   bson_append_utf8(&b, "hello", -1, "world", -1);

   r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error);
   assert(r);

   mongoc_collection_destroy(collection);
   mongoc_client_destroy(client);
   bson_destroy(&b);

   ha_replica_set_shutdown(replica_set);
   ha_replica_set_destroy(replica_set);
}
Exemplo n.º 23
0
static void
test_has_collection (void)
{
   mongoc_collection_t *collection;
   mongoc_database_t *database;
   mongoc_client_t *client;
   bson_error_t error;
   char *name;
   bool r;
   bson_oid_t oid;
   bson_t b;

   client = mongoc_client_new (gTestUri);
   assert (client);

   name = gen_collection_name ("has_collection");
   collection = mongoc_client_get_collection (client, "test", name);
   assert (collection);

   database = mongoc_client_get_database (client, "test");
   assert (database);

   bson_init (&b);
   bson_oid_init (&oid, NULL);
   bson_append_oid (&b, "_id", 3, &oid);
   bson_append_utf8 (&b, "hello", 5, "world", 5);
   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &b, NULL,
                                 &error);
   if (!r) {
      MONGOC_WARNING ("%s\n", error.message);
   }
   assert (r);
   bson_destroy (&b);

   r = mongoc_database_has_collection (database, name, &error);
   assert (!error.domain);
   assert (r);

   bson_free (name);
   mongoc_database_destroy (database);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
}
Exemplo n.º 24
0
Arquivo: main.c Projeto: zhangxj/esbi
int main (int argc,char *argv[])
{
   struct mongo_backend *conf = be_mongo_init();
   mongoc_collection_t *collection;
   collection = mongoc_client_get_collection (conf->client, "test", "p");
      //char *pwd = be_mongo_getuser(conf, "service");
      //printf("%s\n", pwd);

   bson_error_t error;
   bson_t b;

  bson_init(&b);
  bson_append_utf8(&b, "hello", -1, "/world", -1);

    while(1){
      mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error);
    }

  bson_destroy(&b);

    return 0;
}
Exemplo n.º 25
0
static int mongo_set_secret(u08bits *secret, u08bits *realm) {
  mongoc_collection_t * collection = mongo_get_collection("turn_secret"); 

	if(!collection)
    return -1;
    
  bson_t query;
  bson_init(&query);
  BSON_APPEND_UTF8(&query, "realm", (const char *)realm);
  BSON_APPEND_UTF8(&query, "value", (const char *)secret);

  int res = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &query, NULL, NULL);
  mongoc_collection_destroy(collection);
  bson_destroy(&query);

  if (!res) {
    TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Error inserting/updating secret key information\n");
    return -1;
  } else {
    return 0;
  }
}
Exemplo n.º 26
0
int telecommunicating_mongo_insert (centernode_t *cn, cnaccess_t* ca, cdnmsg_t *req)
{
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_t *doc;
    char docbuf[256] = {0};
    sprintf (docbuf, "{\"type\":%d,\"nodeid\":%d,\"addr\":%d,\"switch\":%d,\"time\":\"20%02d-%02d-%02d %02d:%02d:%02d.%03d\"}",
            ntohs(req->pdu.cmd), ntohs(req->pdu.body.login.nodeid),
            ntohs(req->pdu.telemetryreq.addr),
            ntohl(req->pdu.telemetryreq.status),
            (uint8_t)req->pdu.telemetryreq.time[0],
            (uint8_t)req->pdu.telemetryreq.time[1],
            (uint8_t)req->pdu.telemetryreq.time[2],
            (uint8_t)req->pdu.telemetryreq.time[3],
            (uint8_t)req->pdu.telemetryreq.time[4],
            (uint8_t)req->pdu.telemetryreq.time[5],
            (uint8_t)req->pdu.telemetryreq.time[6]);

    doc = bson_new_from_json((uint8_t*)docbuf, strlen(docbuf), &error);
    if (strcmp(error.message,"") != 0) {
        fprintf (stderr, "%s\n", error.message);
        return 0;
    }

    char szaddr[128] = {0};
    sprintf (szaddr, "%x_%x_%x", ntohs(req->pdu.body.login.nodeid),ntohs(req->pdu.telemetryreq.addr),ntohs(req->pdu.cmd));
//    sprintf (szaddr, "%x", ntohs(req->pdu.telemetryreq.addr));
    pthread_mutex_lock (&cn->Mongo[0].MongoLock);
    //    collection = cn->Mongo[0].MongoCollection;
    collection = mongoc_client_get_collection (cn->Mongo[0].MongoClient, "mydb", szaddr);
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
    }   
    mongoc_collection_destroy (collection);
    pthread_mutex_unlock (&cn->Mongo[0].MongoLock);
    bson_destroy (doc);
    return 0;
}
Exemplo n.º 27
0
bool TMongoDriver::insert(const QString &collection, const QVariantMap &object)
{
    if (!isOpen()) {
        return false;
    }

    errorCode = 0;
    errorString.clear();
    bson_error_t error;

    mongoc_collection_t *col = mongoc_client_get_collection(mongoClient, qPrintable(dbName), qPrintable(collection));
    bool res = mongoc_collection_insert(col, MONGOC_INSERT_NONE, (bson_t *)TBson::toBson(object).constData(),
                                        nullptr, &error);

    setLastCommandStatus(mongoc_collection_get_last_error(col));
    mongoc_collection_destroy(col);
    if (!res) {
        tSystemError("MongoDB Insert Error: %s", error.message);
        errorCode = error.code;
        errorString = QLatin1String(error.message);
    }
    return res;
}
static void
test_stats (void)
{
   mongoc_collection_t *collection;
   mongoc_client_t *client;
   bson_error_t error;
   bson_iter_t iter;
   bson_t stats;
   bson_t doc = BSON_INITIALIZER;
   bool r;

   client = mongoc_client_new (gTestUri);
   ASSERT (client);

   collection = get_test_collection (client, "test_stats");
   ASSERT (collection);

   r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error);
   assert (r);

   r = mongoc_collection_stats (collection, NULL, &stats, &error);
   assert (r);

   assert (bson_iter_init_find (&iter, &stats, "ns"));

   assert (bson_iter_init_find (&iter, &stats, "count"));
   assert (bson_iter_as_int64 (&iter) >= 1);

   bson_destroy (&stats);

   r = mongoc_collection_drop (collection, &error);
   assert (r);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   bson_destroy (&doc);
}
Exemplo n.º 29
0
static void
insert_test_docs (mongoc_collection_t *collection)
{
   mongoc_write_concern_t *write_concern;
   bson_error_t error;
   bson_oid_t oid;
   bson_t b;
   int i;

   write_concern = mongoc_write_concern_new();
   mongoc_write_concern_set_w(write_concern, 3);

   {
      const bson_t *wc;
      char *str;

      wc = _mongoc_write_concern_get_gle(write_concern);
      str = bson_as_json(wc, NULL);
      fprintf(stderr, "Write Concern: %s\n", str);
      bson_free(str);
   }

   for (i = 0; i < 200; i++) {
      bson_init(&b);
      bson_oid_init(&oid, NULL);
      bson_append_oid(&b, "_id", 3, &oid);

      ASSERT_OR_PRINT (mongoc_collection_insert(collection,
                                                MONGOC_INSERT_NONE,
                                                &b,
                                                write_concern,
                                                &error), error);
      bson_destroy(&b);
   }

   mongoc_write_concern_destroy(write_concern);
}
Exemplo n.º 30
0
int database_add_orphan_transaction(struct database* db, unsigned char const* hash, struct transaction const* tx)
{
    mongoc_collection_t* collection = mongoc_client_get_collection(db->client, database_name(db), "transactions");
    
    // Convert the transaction to a bson document
    bson_t* doc = bson_new();
    transaction_bson(tx, doc);

    // Set the hash
    BSON_APPEND_BINARY(doc, "hash", BSON_SUBTYPE_BINARY, (uint8_t*)hash, 32);

    // Give it a new id
    bson_oid_t oid;
    bson_oid_init(&oid, NULL);
    BSON_APPEND_OID(doc, "_id", &oid);

    // Orphan -> height = -1
    BSON_APPEND_INT32(doc, "height", -1);

#if 0
    // Print json
    char* str = bson_as_json(doc, NULL);
    printf("%s\n", str);
    bson_free(str);
#endif

    // Perform insert
    bson_error_t error;
    if(mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error) == 0) {
        printf("MongoDB error: %s\n", error.message);
    }

    bson_destroy(doc);
    mongoc_collection_destroy(collection);
    return 0;
}