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); }
void _mongoc_write_command_insert_append (mongoc_write_command_t *command, const bson_t *document) { bson_iter_t iter; bson_oid_t oid; bson_t tmp; ENTRY; BSON_ASSERT (command); BSON_ASSERT (command->type == MONGOC_WRITE_COMMAND_INSERT); BSON_ASSERT (document); BSON_ASSERT (document->len >= 5); /* * If the document does not contain an "_id" field, we need to generate * a new oid for "_id". */ if (!bson_iter_init_find (&iter, document, "_id")) { bson_init (&tmp); bson_oid_init (&oid, NULL); BSON_APPEND_OID (&tmp, "_id", &oid); bson_concat (&tmp, document); _mongoc_buffer_append (&command->payload, bson_get_data (&tmp), tmp.len); bson_destroy (&tmp); } else { _mongoc_buffer_append ( &command->payload, bson_get_data (document), document->len); } command->n_documents++; EXIT; }
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_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); }
/** * _mongoc_gridfs_file_new: * * Create a new empty gridfs file */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new (mongoc_gridfs_t *gridfs, mongoc_gridfs_file_opt_t *opt) { mongoc_gridfs_file_t *file; mongoc_gridfs_file_opt_t default_opt = { 0 }; ENTRY; BSON_ASSERT (gridfs); if (!opt) { opt = &default_opt; } file = (mongoc_gridfs_file_t *)bson_malloc0 (sizeof *file); file->gridfs = gridfs; file->is_dirty = 1; if (opt->chunk_size) { file->chunk_size = opt->chunk_size; } else { /* * The default chunk size is now 255kb. This used to be 256k but has been * reduced to allow for them to fit within power of two sizes in mongod. * * See CDRIVER-322. */ file->chunk_size = (1 << 18) - 1024; } file->files_id.value_type = BSON_TYPE_OID; bson_oid_init (&file->files_id.value.v_oid, NULL); file->upload_date = time (NULL) * 1000; if (opt->md5) { file->md5 = bson_strdup (opt->md5); } if (opt->filename) { file->filename = bson_strdup (opt->filename); } if (opt->content_type) { file->content_type = bson_strdup (opt->content_type); } if (opt->aliases) { bson_copy_to (opt->aliases, &(file->aliases)); } if (opt->metadata) { bson_copy_to (opt->metadata, &(file->metadata)); } RETURN (file); }
QString TBson::generateObjectId() { bson_oid_t oid; bson_oid_init(&oid, NULL); QByteArray oidhex = QByteArray((char *)&oid, sizeof(oid)).toHex(); return QLatin1String(oidhex.data()); }
static void test_bson_oid_init (void) { bson_context_t context; bson_oid_t oid; bson_oid_t oid2; int i; bson_context_init(&context, BSON_CONTEXT_NONE); bson_oid_init(&oid, &context); for (i = 0; i < 10000; i++) { bson_oid_init(&oid2, &context); assert(FALSE == bson_oid_equal(&oid, &oid2)); assert(0 > bson_oid_compare(&oid, &oid2)); bson_oid_copy(&oid2, &oid); } }
static void test_bson_oid_get_time_t (void) { bson_context_t context; bson_oid_t oid; bson_oid_t oid2; /* * Test that the bson time_t matches the current time. This can race, but * i dont think that matters much. */ bson_context_init(&context, BSON_CONTEXT_NONE); bson_oid_init(&oid, &context); bson_oid_init(&oid2, &context); assert(bson_oid_get_time_t(&oid) == bson_oid_get_time_t(&oid2)); assert(time(NULL) == bson_oid_get_time_t(&oid2)); }
static void test_split_insert (void) { mongoc_write_command_t command; mongoc_write_result_t result; mongoc_collection_t *collection; mongoc_client_t *client; bson_oid_t oid; bson_t **docs; bson_t reply = BSON_INITIALIZER; bson_error_t error; int i; bool r; client = test_framework_client_new (NULL); assert (client); collection = get_test_collection (client, "test_split_insert"); assert (collection); docs = bson_malloc (sizeof(bson_t*) * 3000); for (i = 0; i < 3000; i++) { docs [i] = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_OID (docs [i], "_id", &oid); } _mongoc_write_result_init (&result); _mongoc_write_command_init_insert (&command, (const bson_t * const *)docs, 3000, true, true); _mongoc_write_command_execute (&command, client, 0, collection->db, collection->collection, NULL, 0, &result); r = _mongoc_write_result_complete (&result, &reply, &error); assert (r); assert (result.nInserted == 3000); _mongoc_write_command_destroy (&command); _mongoc_write_result_destroy (&result); r = mongoc_collection_drop (collection, &error); assert (r); for (i = 0; i < 3000; i++) { bson_destroy (docs [i]); } bson_free (docs); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void * oid_worker (void *data) { bson_context_t *context = data; bson_oid_t oid; bson_oid_t oid2; int i; bson_oid_init(&oid2, context); for (i = 0; i < 500000; i++) { bson_oid_init(&oid, context); assert(FALSE == bson_oid_equal(&oid, &oid2)); assert(0 < bson_oid_compare(&oid, &oid2)); bson_oid_copy(&oid, &oid2); } return NULL; }
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); }
static void test_bson_oid_get_time_t (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; /* * Test that the bson time_t matches the current time. This can race, but * i dont think that matters much. */ context = bson_context_new (BSON_CONTEXT_NONE); bson_oid_init (&oid, context); bson_oid_init (&oid2, context); BSON_ASSERT (bson_oid_get_time_t (&oid) == bson_oid_get_time_t (&oid2)); BSON_ASSERT (time (NULL) == bson_oid_get_time_t (&oid2)); bson_context_destroy (context); }
I32 __app_guid(lua_State* ls) { bson_oid_t oid; CHAR stroid[25]; LUA_STACK; LUA_WATCH(ls); bson_oid_init(&oid, NULL); bson_oid_to_string(&oid, stroid); lua_pushstring(ls, stroid); LUA_WATCH(ls); return 1; }
bson_document_ref test_create_doc() { struct bson_oid oid; bson_document_builder_ref builder = bson_document_builder_create(); bson_oid_init(&oid); bson_document_builder_append_oid(builder, "_id", &oid); bson_document_builder_append_str(builder, "name", "komnin"); bson_document_builder_append_i(builder, "age", 25); return bson_document_builder_finalize(builder); }
/** * _mongoc_gridfs_file_new: * * Create a new empty gridfs file */ mongoc_gridfs_file_t * _mongoc_gridfs_file_new (mongoc_gridfs_t *gridfs, mongoc_gridfs_file_opt_t *opt) { mongoc_gridfs_file_t *file; mongoc_gridfs_file_opt_t default_opt = { 0 }; ENTRY; BSON_ASSERT (gridfs); if (!opt) { opt = &default_opt; } file = bson_malloc0 (sizeof *file); file->gridfs = gridfs; file->is_dirty = 1; if (opt->chunk_size) { file->chunk_size = opt->chunk_size; } else { /** default chunk size is 256k */ file->chunk_size = 2 << 17; } bson_oid_init (&file->files_id, NULL); file->upload_date = time (NULL) * 1000; if (opt->md5) { file->md5 = bson_strdup (opt->md5); } if (opt->filename) { file->filename = bson_strdup (opt->filename); } if (opt->content_type) { file->content_type = bson_strdup (opt->content_type); } if (opt->aliases) { bson_copy_to (opt->aliases, &(file->aliases)); } if (opt->metadata) { bson_copy_to (opt->metadata, &(file->metadata)); } RETURN (file); }
static void test_bson_oid_init (void) { bson_context_t *context; bson_oid_t oid; bson_oid_t oid2; int i; context = bson_context_new(BSON_CONTEXT_NONE); bson_oid_init(&oid, context); for (i = 0; i < 10000; i++) { bson_oid_init(&oid2, context); assert(FALSE == bson_oid_equal(&oid, &oid2)); assert(0 > bson_oid_compare(&oid, &oid2)); bson_oid_copy(&oid2, &oid); } bson_context_destroy(context); /* * Test that the shared context works. */ bson_oid_init(&oid, NULL); BSON_ASSERT(bson_context_get_default()); }
static void test_oid (void) { bson_oid_t oid; const bson_oid_t *ooid; bson_oid_init (&oid, NULL); bson_t *bcon = BCON_NEW ("foo", BCON_OID (&oid)); assert (BCON_EXTRACT (bcon, "foo", BCONE_OID (ooid))); assert (bson_oid_equal (&oid, ooid)); bson_destroy (bcon); }
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); }
void HHVM_METHOD(MongoDBBsonObjectID, __construct, const Variant &objectId) { MongoDBBsonObjectIDData* data = Native::data<MongoDBBsonObjectIDData>(this_); const String& str_objectId = objectId.isNull() ? null_string : objectId.toString(); if (!objectId.isNull()) { if (bson_oid_is_valid((HHVM_FN(strtolower)(str_objectId)).c_str(), str_objectId.length())) { bson_oid_init_from_string(&data->m_oid, (HHVM_FN(strtolower)(str_objectId)).c_str()); } else { throw MongoDriver::Utils::throwInvalidArgumentException("Error parsing ObjectID string: " + str_objectId); } } else { bson_oid_init(&data->m_oid, NULL); } this_->o_set(s_MongoBsonObjectID_oid, Variant(oidAsString(data)), s_MongoBsonObjectID_className); }
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; }
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_dbpointer (void) { const char *collection; bson_oid_t oid; const bson_oid_t *ooid; bson_oid_init (&oid, NULL); bson_t *bcon = BCON_NEW ("foo", BCON_DBPOINTER ("collection", &oid)); assert (BCON_EXTRACT (bcon, "foo", BCONE_DBPOINTER (collection, ooid))); assert (strcmp (collection, "collection") == 0); assert (bson_oid_equal (ooid, &oid)); bson_destroy (bcon); }
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); }
static void test_oid (void) { bson_t bcon, expected; bson_oid_t oid; bson_init (&bcon); bson_init (&expected); bson_oid_init (&oid, NULL); bson_append_oid (&expected, "foo", -1, &oid); BCON_APPEND (&bcon, "foo", BCON_OID (&oid)); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); }
static void test_save (void) { 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); collection = mongoc_client_get_collection(client, "test", "test"); 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_save(collection, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } bson_destroy (&b); mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }
void _mongoc_write_command_insert_append (mongoc_write_command_t *command, const bson_t *document) { const char *key; bson_iter_t iter; bson_oid_t oid; bson_t tmp; char keydata [16]; ENTRY; BSON_ASSERT (command); BSON_ASSERT (command->type == MONGOC_WRITE_COMMAND_INSERT); BSON_ASSERT (document); BSON_ASSERT (document->len >= 5); key = NULL; bson_uint32_to_string (command->n_documents, &key, keydata, sizeof keydata); BSON_ASSERT (key); /* * If the document does not contain an "_id" field, we need to generate * a new oid for "_id". */ if (!bson_iter_init_find (&iter, document, "_id")) { bson_init (&tmp); bson_oid_init (&oid, NULL); BSON_APPEND_OID (&tmp, "_id", &oid); bson_concat (&tmp, document); BSON_APPEND_DOCUMENT (command->documents, key, &tmp); bson_destroy (&tmp); } else { BSON_APPEND_DOCUMENT (command->documents, key, document); } command->n_documents++; EXIT; }
static void test_dbpointer (void) { bson_t bcon, expected; bson_oid_t oid; bson_init (&bcon); bson_init (&expected); bson_oid_init (&oid, NULL); bson_append_dbpointer (&expected, "foo", -1, "collection", &oid); BCON_APPEND (&bcon, "foo", BCON_DBPOINTER ("collection", &oid)); bson_eq_bson (&bcon, &expected); bson_destroy (&bcon); bson_destroy (&expected); }
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); }
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; }