+ pthread_mutex_lock(&node->lock);
+ if (wm_initialize(node) < 0) {
+ ERROR("write_mongodb plugin: error making connection to server");
+ pthread_mutex_unlock(&node->lock);
+ bson_free(bson_record);
+ return -1;
+ }
+
+ collection =
+ mongoc_client_get_collection(node->client, "collectd", vl->plugin);
+ if (!collection) {
+ ERROR("write_mongodb plugin: error creating/getting collection");
+ mongoc_database_destroy(node->database);
+ mongoc_client_destroy(node->client);
+ node->database = NULL;
+ node->client = NULL;
+ node->connected = 0;
+ pthread_mutex_unlock(&node->lock);
+ bson_free(bson_record);
+ return -1;
+ }
+
+ status = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, bson_record,
+ NULL, &error);
+
+ if (!status) {
+ ERROR("write_mongodb plugin: error inserting record: %s", error.message);
+ mongoc_database_destroy(node->database);
+ mongoc_client_destroy(node->client);
+ node->database = NULL;
+ node->client = NULL;
+ node->connected = 0;
+ pthread_mutex_unlock(&node->lock);
+ bson_free(bson_record);
+ mongoc_collection_destroy(collection);
+ return -1;
+ }